Desktop productivity for business analysts and programmers

flexible macro names

Reply
N/A
Posts: 0

flexible macro names

Hi
I am wanting to assign different macro variables to an array. With help from this forum, I have the assignment of the macro variable working in principle. However, I would like to change which macro variables are assigned to this array (i.e. "&adjust0904 &adjust0906" in the sample code below). I cannot figure out the code to get this working. Any ideas? Thanks!

%Macro Survey(Months);

%Let adjust0904 = 1.1;
%Let adjust0905 = 1.2;
%Let adjust0906 = 1.3;

Data B;
Set survey;

* &Months should be incorporated some how to provide the code below: ;
array aj{2} _temporary_ (&adjust0904 &adjust0906);

Do i = 1 to 2;
...code...
End;
Run;
%Mend;
%Survey (0904 0905);
SAS Super FREQ
Posts: 8,720

Re: flexible macro names

Hi:
You will want to read about referencing macro variables "indirectly", as explained in this documentation topic:
http://support.sas.com/documentation/cdl/en/mcrolref/61885/HTML/default/a001071915.htm

Basically, the way you reference macro variables indirectly is to allow a "final" string to be resolved from multiple macro variable references. Consider the macro program at the bottom of this posting for an example of indirect macro references.

These papers may also be useful to you:
http://www2.sas.com/proceedings/sugi29/128-29.pdf
http://www2.sas.com/proceedings/sugi22/CODERS/PAPER77.PDF
http://www.lexjansen.com/sugi/sugi21/bt/054-21.pdf
http://www.ats.ucla.edu/stat/sas/library/nesug98/p086.pdf
http://www.nesug.org/Proceedings/nesug97/posters/gerlach.pdf
http://studysas.blogspot.com/2009/02/3when-to-use-do-we-distinguishmultiple.html

cynthia

[pre]
%let fav = muppet;
%let muppet1 = kermit;
%let muppet2 = elmo;
%let muppet3 = big bird;

%macro listmup2;
%do i = 1 %to 3;
%put My favorite &fav number &i is: &&muppet&i;
%put Alternate: My favorite &fav is: &&&fav&i;
%end;
%mend listmup2;

%listmup2;
[/pre]
Super Contributor
Posts: 359

Re: flexible macro names

You can parse the incoming parameter then append it to the root of your macro variable.

%macro survey( parm);

%Let adjust0904 = 1.1;
%Let adjust0905 = 1.2;
%Let adjust0906 = 1.3;


%let i = 1;
%do %while (%scan(&parm, &i, ' ') ^= );
%let cd = %scan(&parm, &i, ' ');
%put &&adjust&cd;
%let i = %eval(&i + 1);
%end;
%mend;
%survey(0904 0906);
N/A
Posts: 0

Re: flexible macro names

This is fantastic - thanks to you both!! The following code in the data step does the trick:
[pre]
%let adjust = ;

%let i = 1;
%do %while (%scan(&parm, &i, ' ') ^= );
%let cd = %scan(&parm, &i, ' ');
%let adjust = &adjust &&adjust&cd;
%put &adjust;
%let i = %eval(&i + 1);

%end;

array aj{3} _temporary_ (&adjust);
[/pre]
Ask a Question
Discussion stats
  • 3 replies
  • 124 views
  • 0 likes
  • 3 in conversation