BookmarkSubscribeRSS Feed
kadag68
Calcite | Level 5

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;

3 REPLIES 3
ChrisNZ
Tourmaline | Level 20

Create a variable 

%let fileno=%sysfunc(putn(&i,z4.));

ChrisNZ
Tourmaline | Level 20

Also note that proc append would be more efficient than recreating the data set 100 times.

34reqrwe
Quartz | Level 8

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;

 

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 548 views
  • 0 likes
  • 3 in conversation