Pages

SyntaxHighlighter

Wednesday, January 8, 2020

Importing a delimited file

As it turns out, this is my 100th blog post since I started doing this back in November 2011. I create these posts as a service to other SAS users that might find some value in things that I have encountered as an issue and found a solution or just pointing out another way to do things.

This post is about a very clever and efficient way to import a delimited file. Credit for this idea goes out to my sagacious co-worker, Dave Devoll.

Let's assume you have a file of comma separated values (CSV) that should be imported into a data set with the same structure as the sashelp.class data set. The use of the IF 0 THEN SET dataset command compiles but never executes so it creates the program data vector (PDV) values. The input (_all_) references the PDV while the (:) modifier works as a separator between the values.

Contents of the cls.csv file
Name,Sex,Age,Height,Weight
Alfred,M,14,69,112.5
Alice,F,13,56.5,84
Barbara,F,13,65.3,98
Carol,F,14,62.8,102.5
Henry,M,14,63.5,102.5
data temp;
  infile '/temp/cls.csv' dsd dlm = ',' firstobs = 2;
  if 0 then set sashelp.class;  /* prime the program data vector */
  input (_all_) (:);            /* read in all the columns from the PDV */
run;

Now if you want to add the data to an existing data set, use proc append and you are done..