51sep
51sep

Reputation: 489

How to import a raw data using "input" in SAS

I'd like to import a raw data using "input" in SAS. My following program doesn't work well. How do I do that? Please give me some advice.

data dt00;
infile datalines;
input Year School & $27. Enrolled : comma.;
datalines;
1868 U OF CALIFORNIA BERKELEY 31,612
1906 U OF CALIFORNIA DAVIS 21,838
1965 U OF CALIFORNIA IRVINE 15,874
1919 U OF CALIFORNIA LOS ANGELES 35,730
;
run;

Upvotes: 2

Views: 77

Answers (2)

Tom
Tom

Reputation: 51621

The & modifier in your input statement says to look for two or more delimiters in a row to mark the end of the next "word" in the line. Make sure the lines of data actually have the extra space. Also make sure to include the : modifier in front of any informat specification in the INPUT statement.

data dt00;
  input Year School & :$27. Enrolled : comma.;
datalines;
1868 U OF CALIFORNIA BERKELEY    31,612
1906 U OF CALIFORNIA DAVIS   21,838
1965 U OF CALIFORNIA IRVINE   15,874
1919 U OF CALIFORNIA LOS ANGELES   35,730
;

Upvotes: 1

Stu Sztukowski
Stu Sztukowski

Reputation: 12909

datalines is space-separated by default. You can specify specific line lengths as you are doing and do additional post-processing cleanup, but the easiest thing to do is add a different delimiter and include the dlm option in your infile statement.

data dt00;
    infile datalines dlm='|';

    length Year 8. School $27. Enrolled 8.;

    input Year School$ Enrolled : comma.;

    datalines;
    1868|U OF CALIFORNIA BERKELEY|31,612
    1906|U OF CALIFORNIA DAVIS|21,838
    1965|U OF CALIFORNIA IRVINE|15,874
    1919|U OF CALIFORNIA LOS ANGELES|35,730
    ;
run;

Output:

Year    School                       Enrolled
1868    U OF CALIFORNIA BERKELEY     31612
1906    U OF CALIFORNIA DAVIS        21838
1965    U OF CALIFORNIA IRVINE       15874
1919    U OF CALIFORNIA LOS ANGELES  35730

SAS has a ton of options on the input statement for reading both structured and unstructured data, but at the end of the day, it's easiest to get it in a delimited format whenever possible.

Upvotes: 0

Related Questions