Reading csv files in SAS

SAS reads any input file (ASCII) separated by delimited characters. However, it is important to assign the format to the SAS variables to ensure that the columns are read correctly.

The following are to be established to ensure correct reading of the files.

  1. DLM -> ‘,’ OR ‘|’ or any other characters used as delimiter.
  2. DSD -> indicates that the data is sensitive and may contain delimiters between the quotes (string data) and need to be considered as characters.
  3. TRUNCOVER -> Assign the value to the variable even if the value is less than the allocate format size.
  4. LRECL -> default logical record length for reading an external file
  5. FIRSTOBS -> read from specified row number
  6. FORMAT -> :$20.

The most important is the : . The colon indicates that the reading of columns begin with the delimiter and must not consider the length (20 in this case) to separate the columnar data. $ is to indicate that it is a character. ($w.) w is the width of the characters.

Following is an example:

 

DATA csv_data;

INFILE ‘C:\path_to_dir\file_name.csv’ DSD DLM=’,’ TRUNCOVER LRECL=1024 FIRSTOBS=2;

INPUT char_variable1 :$20. char_variable2:$20. char_variable3:$20. int_variable 5.3;

RUN;