Hi All,
I have mutitlple SAS data sets with names file91 to file14. The last two digits represents years (e.g.91 represents 1991 and 00 represents 2000). I tried to run a macro to generate some results by reading these names. However, I failed to concanate file names within the macro. Could you one help? Thanks.
%macro getall;
%do i=91 %to 99; /*year 1991 to 1999*/
%getpart1(file&i);
%end;
%do i=0 %to 14; /*year 2000 to 2014*/
%getpart2(%sysfunc(cats('file',put(&i,z2.))));
%end;
%mend getall;
%getall
Assuming that somewhere you have properly defined %GETPART1 and %GETPART2, your top loop should be fine (Of course I'm not yet convinced that you have defined those other macros, but you'll be able to comment about that easily enough.)
The bottom loop requires a few changes, though. CATS is not needed to concatenate strings in macro language, nor are quotes needed to define a character string. And %SYSFUNC cannot be applied to PUT (only to PUTN or PUTC). So you would need:
%do i=0 %to 14;
%getpart2 (file%sysfunc(putn(&i, z2)))
%end;
Assuming that somewhere you have properly defined %GETPART1 and %GETPART2, your top loop should be fine (Of course I'm not yet convinced that you have defined those other macros, but you'll be able to comment about that easily enough.)
The bottom loop requires a few changes, though. CATS is not needed to concatenate strings in macro language, nor are quotes needed to define a character string. And %SYSFUNC cannot be applied to PUT (only to PUTN or PUTC). So you would need:
%do i=0 %to 14;
%getpart2 (file%sysfunc(putn(&i, z2)))
%end;
Thanks Astounding. This is exactly what I want! I shouldn't put the concatenate function or quotes to concatenate string in macro! This sloved my question!
You can also use a single loop:
%macro getall;
%do year=1991 %to 2014;
%put file%substr(&year, 3, 2);
%end;
%mend getall;
%getall;
Firstly, I would always suggest to combine all "like" data together in one dataset - with a date column for the year if need be. This will considerably shrink your programming effort and make your life much easier.
You can of course loop by using:
data _null_; set sashelp.vtable (where=(libname="WORK" and substr(memname,1,4)="FILE")); /* This bit gets generated for each filexx dataset */ call execute('%getpart1 ('||strip(memname)||');'); call execute('%getpart2 ('||strip(memname)||');'); run;
You don't need any hardcoding or loops then of the files, as anything in work with the prefix file will be processed.
I would still recommend combining them though.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.