user27008
user27008

Reputation: 610

Manually reading in a specific section of a csv file into SAS

I want to read a csv file into SAS, but I only want to read in part of the file. For example, I want my first row of data to start at row 18, while I want to read in columns 9, 11, 12, 13, 19, 20, 36. Is there an efficient way of doing this manually in a data step to read in the file portions I want, or is my best bet just to read in the entire file using the import wizard and just keep the columns of desire?

Upvotes: 3

Views: 4020

Answers (2)

MDe
MDe

Reputation: 2508

If you can use variable names instead of column numbers, this will work. I'd recommend using variable names instead of numbers anyways, as it adds substantive meaning to your code and might help you catch a problem if the input file columns are ever changed.

PROC IMPORT datafile = "filename.csv"
    out = data_read (keep = var1 var2 var3)
    dbms = csv
    replace;
    datarow = 18;
RUN;

Upvotes: 1

Joe
Joe

Reputation: 63424

You can change the row you start with the DATAROW option on PROC IMPORT, or FIRSTOBS option on a data step input.

You cannot easily read in only select columns, however. You would have to read in all columns up to the last column you are interested in, and then drop the uninteresting ones. You could read them all in with a $1 character called "blank" or something (even the same name each time), but you do have to ask for them.

The only workaround would be to write a regular expression to read in your data, in which case you could tell it to look for ,.*?,.*?, etc. for each skipped column.

Upvotes: 6

Related Questions