BookmarkSubscribeRSS Feed
emaguin
Quartz | Level 8

I'm very, very new with SAS. How do i read a csv file with variable names in line 1 and a lrecl of 132K in round numbers? I've tried proc import but it seems limited to a 32K+ lrecl. There seems to be an infile command required but i'm not sure how to set that up and, if really required, I need guidance in very specific terms. A data step seems to be an alternative but as i understand it, that infile command is required and worse, far worse, a Data step is not able to read line 1 as variable names. I have about 1200 of them, mainly numeric but also string and of varying lengths, and i absolutely do not want to retype them. I know sas can read excel files directly via the "engine" but can that command read a file of that width and recognize line 1 as variable names? I've tried the import wizard the lrecl is a problem.

 

Thanks, Gene Maguin

2 REPLIES 2
Tom
Super User Tom
Super User

It is never required to use PROC IMPORT to read a text file.

Do you have some other source that describes what is in that file?  If so use that to generate the code to read your file.

 

If not then just copy the first line and use it to define your variables.   If you cannot figure out how to copy the first line then use a program like this to list them into the SAS LOG so you can copy and paste from there.

data _null_;
  infile 'myfile.txt' dsd obs=1 ;
  input name :$32. @@;
  put name ;
run;

 If you are unsure of how to define some of them then define them as reasonably long character variables and then examine the  resulting dataset and adjust your program later.

If you really have 1,200 variables then hopefully you can just define a lot of them using variable lists instead of having to list all 1200 variables.

So you will end up with a data step that looks like this.

 

data want ;
  infile 'myfile.txt' dsd truncover firstobs=2;
  length
    id $20
    var1-var1200 8
  ;
  input id -- var1200 ;
run;   
 

If you have any date, time or datetime values then add INFORMAT and FORMAT statements to tell SAS how to conver the text into values and how to display the values by converting the values into text.   Most variables will not need either an informat or a format attached.

ballardw
Super User

@emaguin wrote:

I'm very, very new with SAS. How do i read a csv file with variable names in line 1 and a lrecl of 132K in round numbers? I've tried proc import but it seems limited to a 32K+ lrecl. There seems to be an infile command required but i'm not sure how to set that up and, if really required, I need guidance in very specific terms. A data step seems to be an alternative but as i understand it, that infile command is required and worse, far worse, a Data step is not able to read line 1 as variable names. I have about 1200 of them, mainly numeric but also string and of varying lengths, and i absolutely do not want to retype them. I know sas can read excel files directly via the "engine" but can that command read a file of that width and recognize line 1 as variable names? I've tried the import wizard the lrecl is a problem.

 

Thanks, Gene Maguin


When you say "numeric" do you mean the first line actually contains a number that is supposed to be the "variable name"?

If any of those "variable names" in line 1 have punctuation, space or special characters then they cannot be used as SAS variables generally.

You can get a nice column of variables by opening that CSV in your spreadsheet, highlight the cells in the first row, copy and then "paste transpose" into a different sheet of a spread sheet. Then you can copy that transposed text and paste into the SAS editor as an informat  and input statements that would look like

  informat
      SUMLEV
      REGION
      DIVISION
      STATE
      StateName
      SEX
      ORIGIN
      RACE
      AGE
      CENSUS2010POP
      ESTIMATESBASE2010
  ;
   input
      SUMLEV
      REGION
      DIVISION
      STATE
      StateName
      SEX
      ORIGIN
      RACE
      AGE
      CENSUS2010POP
      ESTIMATESBASE2010
   ;

After each variable in the informat statement you can provide the appropriate informat $25 to read a 25 character field for example, or MMDDYY10. to read a date that looks like 01/25/2019, or   12. to read a numeric value of up to 12 digits, or Comma to read currency.

 

If your variable names contain special character and such then need to change them in the above so that they follow SAS naming rules: start with a letter or _ and have a maximum of 32 character using only letters, digits or _ characters.

 

You need to provide the infile statement with the filename delimiter and record length.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 937 views
  • 0 likes
  • 3 in conversation