macro parameter with concatenate names

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 7
Accepted Solution

macro parameter with concatenate names

[ Edited ]

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

 


Accepted Solutions
Solution
‎05-10-2017 12:50 PM
Super User
Posts: 5,071

Re: macro parameter with concatenate names

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;

 

View solution in original post


All Replies
Solution
‎05-10-2017 12:50 PM
Super User
Posts: 5,071

Re: macro parameter with concatenate names

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;

 

Occasional Contributor
Posts: 7

Re: macro parameter with concatenate names

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!Smiley Wink

Super User
Posts: 17,743

Re: macro parameter with concatenate names

You can also use a single loop:

 

%macro getall;
    %do year=1991 %to 2014;
        %put file%substr(&year, 3, 2);
    %end;
%mend getall;

%getall;
Super User
Super User
Posts: 7,392

Re: macro parameter with concatenate names

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.

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 153 views
  • 2 likes
  • 4 in conversation