Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Inputing SAS Data

Status
Not open for further replies.

roland9

Technical User
Feb 8, 2006
17
0
0
US
Hi,

I am a beginner in sas .Can someone tell me how to input this raw data file into sas .The fields are ID,Name, City, State, Sex and Code

1333,BLAIR,JUSTIN,STAMFORD,CT,M,PT2,
1428,BRADY,CHRISTINE,STAMFORD,CT,F,PT1,
1439,HARRISON,FELICIA,BRIDGEPORT,CT,F,PT1,
1545,HUNTER,CLYDE,STAMFORD,CT,M,PT1,
1333,NEWKIRK,SANDRA,PRINCETON,NJ,F,PT2,
1478,NEWTON,JAMES,NEW YORK,NY,M,PT2,

Thanks,
 
Simplest way:-
Code:
data mydat;
  infile cards dlm=',' dsd;

  length ID       $4
         surname  $20
         forename $20
         City     $20
         State    $2
         Sex      $1
         Code     $3;

  input ID
        surname
        forename
        city
        state
        sex
        code;

cards4;
1333,BLAIR,JUSTIN,STAMFORD,CT,M,PT2,
1428,BRADY,CHRISTINE,STAMFORD,CT,F,PT1,
1439,HARRISON,FELICIA,BRIDGEPORT,CT,F,PT1,
1545,HUNTER,CLYDE,STAMFORD,CT,M,PT1,
1333,NEWKIRK,SANDRA,PRINCETON,NJ,F,PT2,
1478,NEWTON,JAMES,NEW YORK,NY,M,PT2,
;;;;
run;
The DSD option allows for missing variables which result in two delimiters next to each other. Without this option, if you have this scenario:-
1333,,JUSTIN,STAMFORD,CT,M,PT2
Surname will be read in as "JUSTIN" and Surname as "STAMFORD" etc.

Cards in the infile statement tells SAS that the data is inline with the code rather than an external file. IF the data is actually in an external file, set up a filename statement and replace "cards" in the infile statement with your filename.
Cards4 works the same as a CARDS statement, telling SAS that this is where the inline data starts. The difference is that whereas CARDS tells SAS to finish reading the data when it encounters a single semicolon, CARDS4 waits for 4 semicolons. This allows for semicolons in your data.

If you want ID to be numeric, then change the length statement to
Code:
  length ID    8
         surname  $20
         forename $20
         City     $20
         State    $2
         Sex      $1
         Code     $3;
However, it's good practice to set up IDs as character variables as they can a - be made smaller (4 bytes instead of 8), and b - do not suffer from accuracy loss if they are extremely long IDs like bank account numbers.

Enjoy.
 
Hey Chris,

Thanks for the reply .Well what if I want surname and forename under one variable NAME (Eg Justin Blair, Christine Brady...) Is that possible and how would the code change for that

Thanks,
Roland
 
Concatenate them after you read them in. Currently youhave them separated by the same delimiter as the other fields, so you can't read them in together in the current format.
Code:
   name = trim(left(forename)) || ' ' || trim(left(surname));
or
Code:
  name = trim(left(surname)) || ', ' || trim(left(forename));
If you're using version 9 of SAS, look up the CATX function.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top