Input/Infile Statements

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 121
Accepted Solution

Input/Infile Statements

I have the code below which I am using to import the attached .csv file into SAS; I would like to retain whatever variable headings as SAS variable names (not all columns in the .csv file have a title). My biggest issue with the code below is that, as you can see, once imported into SAS, the variable values in the input statement below have their values overlapping with each other. Seeing that my variable values are of varying lengths, my data gets really ugly. Is there a neat way to import my variable values and preserving their values intact?

data WORK.NCDebt;

     infile '/Source/InputFileNCDebt.csv' dsd;

input Var1 $10. Var2 $11-20 Var3 $21-30 Var4 $31-40 Var5 $41-50;

run;

Attachment

Accepted Solutions
Solution
‎09-04-2014 06:15 PM
Super User
Super User
Posts: 6,851

Re: Input/Infile Statements

Since you have a CSV file you do not want to use a column mode input statement.

Now to the question of how to use the fact that VAR1 and VAR3 have a column heading in row one of your input file.

One way would be to read the names in and generate a macro variable with the names.  It is easier if you just hard code how many columns to read.

data _null_;

  infile 'c:\downloads\InputFileNCDebt.csv' dsd obs=1;

  length name $32 namelist $200 ;

  do i=1 to 5 ;

    input name @ ;

    name = coalescec(name,cats('VAR',i));

    namelist=catx(' ',namelist,name);

  end;

  call symputx('namelist',namelist);

run;

 

data WORK.NCDebt;

  infile 'c:\downloads\InputFileNCDebt.csv' dsd firstobs=2;

  length &namelist $10;

  input &namelist ;

run;

proc print width=min;

run;

Obs      CUSIP        VAR2       Instrument       VAR4       VAR5

1    912810QQ4    912810QQ4 BOND          US TREASUR    Govt

2    912810QB7    912810QB7 BOND          US TREASUR    Govt

3    912810PX0    912810PX0 BOND          US TREASUR    Govt

4    912810EJ3    912810EJ3 BOND          US TREASUR    Govt

5    912810EF1    912810EF1 BOND          US TREASUR    Govt

6    3135G0LN1    3135G0LN1 BOND          FANNIE MAE    Corp

7    3135G0HB2    3135G0HB2 BOND          FANNIE MAE    Corp

8    31398AZV7    31398AZV7 BOND          FANNIE MAE    Corp

9    31398AYY2    31398AYY2 BOND          FANNIE MAE    Corp

     10    31359MW41 31359MW41    Agency Deb    FANNIE MAE    Corp

View solution in original post


All Replies
Solution
‎09-04-2014 06:15 PM
Super User
Super User
Posts: 6,851

Re: Input/Infile Statements

Since you have a CSV file you do not want to use a column mode input statement.

Now to the question of how to use the fact that VAR1 and VAR3 have a column heading in row one of your input file.

One way would be to read the names in and generate a macro variable with the names.  It is easier if you just hard code how many columns to read.

data _null_;

  infile 'c:\downloads\InputFileNCDebt.csv' dsd obs=1;

  length name $32 namelist $200 ;

  do i=1 to 5 ;

    input name @ ;

    name = coalescec(name,cats('VAR',i));

    namelist=catx(' ',namelist,name);

  end;

  call symputx('namelist',namelist);

run;

 

data WORK.NCDebt;

  infile 'c:\downloads\InputFileNCDebt.csv' dsd firstobs=2;

  length &namelist $10;

  input &namelist ;

run;

proc print width=min;

run;

Obs      CUSIP        VAR2       Instrument       VAR4       VAR5

1    912810QQ4    912810QQ4 BOND          US TREASUR    Govt

2    912810QB7    912810QB7 BOND          US TREASUR    Govt

3    912810PX0    912810PX0 BOND          US TREASUR    Govt

4    912810EJ3    912810EJ3 BOND          US TREASUR    Govt

5    912810EF1    912810EF1 BOND          US TREASUR    Govt

6    3135G0LN1    3135G0LN1 BOND          FANNIE MAE    Corp

7    3135G0HB2    3135G0HB2 BOND          FANNIE MAE    Corp

8    31398AZV7    31398AZV7 BOND          FANNIE MAE    Corp

9    31398AYY2    31398AYY2 BOND          FANNIE MAE    Corp

     10    31359MW41 31359MW41    Agency Deb    FANNIE MAE    Corp

Super User
Super User
Posts: 7,727

Re: Input/Infile Statements

Or, you could build a small import program, the below for instance assumes text for quoted values and numeric for not:

/* test data CSV looks like this */

/*

TOWN,POP,IN

"London",123456,"UK"

"Glasgow",34243,"UK"

"Brussels",7876,"Belgium"

*/

data read_data;
  length buffer $2000.;
  infile "s:\temp\rob\test.csv" dsd missover lrecl=32767 dlm="¬";
  input buffer $;
run;

data a/*_null_*/;
  merge read_data (obs=1 in=head) read_data (obs=2 in=first_row firstobs=2 rename=(buffer=myrow));
  i=1;
  call execute('data want (drop=buffer);
                  set read_data (firstobs=2);
                  length ');
  /* Assign lengths based on first row */
  do while (scan(buffer,i,",") ne "");
    call execute(" "||scan(buffer,i,","));
    if index(scan(myrow,i,","),'"')>0 then call execute(' $20');
    else call execute(' 8');
    i=i+1;
  end;
  i=1;
  call execute(';');
  /* Split up the data */
  do while (scan(buffer,i,",") ne "");
    call execute(" "||scan(buffer,i,",")||'=scan(buffer,'||strip(put(i,best.))||',",");');
    i=i+1;
  end;
  call execute('; run;');
run;

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 290 views
  • 3 likes
  • 3 in conversation