BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
maroulator
Obsidian | Level 7

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;

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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

2 REPLIES 2
Tom
Super User Tom
Super User

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

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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