1

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.

bill999
  • 2,147
  • 8
  • 51
  • 103

1 Answers1

2

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;
in_user
  • 1,948
  • 1
  • 15
  • 22
  • Thanks so much. This is wonderful! One little thing - does one need the semicolon at the end of the fourth line? – bill999 May 30 '15 at 02:34