1
votes

TOPIC: Change dataset with loop count into append statement

I have a macro that will loop and create a new dataset with a counter behind.

Code like this:

PROC IMPORT OUT=WORK.out&i DATAFILE= "&dir/&name"
/excelout/
DBMS=csv REPLACE; delimiter='09'x; getnames=no; RUN;

data test&i (drop= %do k=1 %to &cnt; &&col&k.. %end;
);
length station $10 voltage $10 year 8 month $20 transformer $10 Day $20 Date Time MW_Imp MW_Exp MVAR_Imp MVAR_Exp MVA Power_Factor 8; format Time hhmm.; set out&i. end=last;

Currently the script will generate about 4 data sets if i have 4 external files by PROC IMPORT.

What i want is to eliminate the creation of multiple datasets but just append them into the master file. Is there a way to do so?

2

2 Answers

0
votes

An append statement inside the loop should be sufficient to achieve this. SAS will copy first dataset as base since it was not existing.

proc append base=test data=test&i force; run;
0
votes

Appending is probably just as easy, but if you don't want to create many datasets to begin with, you could use a data step to read in several files at once, using wildcards. That would eliminate the need to loop through the files, but does require that the files have the same structure and aren't stored in a folder with other similarly named files. The firstobs-option caused som issues in my tests, but as you have specified getnames=no in your import, I guess you have no need for it.

The snippet below inputs all csv files in c:\test.

data test;
    infile "c:\test\*.csv" dsd delimiter='09'x;
    input varA $ varB $;
run;