I need help with a macro for reading in and combining zip files. There are 100 zip files that I need to read into SAS and combine into one dataset. The filenames are of the form: file-00000data.csv.gz, file-00001data.csv.gz,..., file-00099data.csv.gz
The way I have written my macro, Files 00000-00009 need to be handled separately from Files 00010-00099, but I don't now how to create the two cases within the macro.
For the first 10 files, the following macro works:
data combined_data;
set _null_; **empty dataset to add your data to one file at a time;
run;
%macro unzip_data(filenum);
%do i=0 %to &filenum;
filename fromzip zip "&wallet.file-0000&i.data.csv.gz" GZIP;
data newdata;
infile fromzip delimiter = ',' MISSOVER DSD firstobs=2 ;
informat id best32. bucket_1 best32. bucket_2 best32.;
format id best12. bucket_1 best12. bucket_2 best12.;
input id bucket_1 bucket_2;
run;
data combined_data;
set combined_data newdata;
run;
%end;
%mend unzip_data;
However, I am having trouble creating the code for handling all of the files (the below code is not working):
%macro unzip_data(filenum);
%do i=0 %to &filenum;
%if i<10 %then %do;
filename fromzip zip "&wallet.file-0000&i.data.csv.gz" GZIP;
data newdata;
infile fromzip delimiter = ',' MISSOVER DSD firstobs=2 ;
informat id best32. bucket_1 best32. bucket_2 best32.;
format id best12. bucket_1 best12. bucket_2 best12.;
input id bucket_1 bucket_2;
run;
data combined_data;
set combined_data newdata;
run;
%end;
%else %do;
filename fromzip zip "&wallet.file-000&i.data.csv.gz" GZIP;
data newdata;
infile fromzip delimiter = ',' MISSOVER DSD firstobs=2 ;
informat id best32. bucket_1 best32. bucket_2 best32.;
format id best12. bucket_1 best12. bucket_2 best12.;
input id bucket_1 bucket_2;
run;
data combined_data;
set combined_data newdata;
run;
%end;
%end;
%mend unzip_data;
Also note that proc append would be more efficient than recreating the data set 100 times.
ChrisNZ gave a better solution for what you are trying to do, but FYI you were missing an &
%if i<10 %then %do;
/*should be*/
%if &i<10 %then %do;
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.