Loop over strings and read in files and save [duplicate]

倖福魔咒の 提交于 2020-01-05 08:47:06

问题


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;


来源:https://stackoverflow.com/questions/30540552/loop-over-strings-and-read-in-files-and-save

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!