Loop over strings and read in files and save [dupl

2019-08-13 05:57发布

问题:

This question already has an answer here:

  • Append multiple CSV files in SAS 5 answers

I am pretty new to SAS. I have a bunch of files that are in csv format that I want to read into SAS. I need to read them in one-by-one and save them, either individually or preferably in one huge file. (Although the file might be really large...not sure how to best deal with this - MySQL maybe?)

Say the file are named like this:

file97.csv
file98.csv
file99.csv
file00.csv
file01.csv
file02.csv

How can I loop over the 97, 98, 99, 00, 01, 02 in a loop statement?

If I import just, say file97.csv, the code is something like:

PROC IMPORT OUT= WORK.data97 
            DATAFILE= "\...\file97.csv" 
            DBMS=CSV REPLACE;
     GETNAMES=YES;
     DATAROW=2; 
RUN;

What code would I write to loop? I basically need to change only the 97s.

回答1:

Since you want to loop over Proc import you will have to Macros for that, also since you Numbers 97, 98, 99, 00, 01, 02 are not consecutive you will have to use a workaround.

%let files=97,98,99,00,01,02;
%macro loop_over;
%do i=1 %to %sysfunc(countw("&files."));
PROC IMPORT OUT= WORK.data%sysfunc(scan("&files.",&i.,",")) 
            DATAFILE= "\...\file%sysfunc(scan("&files.",&i.,",")).csv" 
            DBMS=CSV REPLACE;
     GETNAMES=YES;
     DATAROW=2; 
RUN;
%end;
%mend;
%loop_over;