DATA Step, Macro, Functions and more

making variable lists in macros flexible to the number of parameters

Reply
N/A
Posts: 0

making variable lists in macros flexible to the number of parameters

I have just started with macros, and I just wrote a set of macros which compares several sets of variables (each base parameter makes three different variables for different years). Right now I have one macro for each possible number of parameters. I want the macro to be flexible enough to work with anywhere between 2 and 10 variable sets.

Here is the beginning of the program for reference:

%Macro sum2p (title=, base1=, base2=);

proc sql;
create table &title.1 as
select provfs,
&base1.y1_f, &base1.y2_f, &base1.y3_f, &base1.y4_f,
&base2.y1_f, &base2.y2_f, &base2.y3_f, &base2.y4_f,
(&base1.y1_f+&base2.y1_f) as &title._sum1,
(&base1.y2_f+&base2.y2_f) as &title._sum2,
(&base1.y3_f+&base2.y3_f) as &title._sum3,
(&base1.y4_f+&base2.y4_f) as &title._sum4
from _2010.facrep;



My best idea is to have a series of %ifs %lets for each parameter so that the ends of the variables and the commas identifying them would only appear when the corresponding parameter is defined, so

&base4.y1_f,

Would become

&base4.&var4y1.&var4comma.


I tried that, but couldn’t get it to work properly. Still, I know there must be a much better way to approach this problem. Could someone tell me how to do this better?
SAS Super FREQ
Posts: 8,743

Re: making variable lists in macros flexible to the number of parameters

Hi:
One of the best practices for coming up with robust and correct macro code is to start with a working SAS program before you add any macro variables with the code. Then, after that first step, your next step is to "macroize" the code by using %LET and hard-coded macro variables. Next, after that code proves to work and you debug any issues with quotes or references, if you need a macro program, you then add the %macro/%mend and create parameters and make certain portions of the code conditional with %IF statements or make certain portions of the code get created in a loop with a %DO loop.

So, my questions to you are...where did you start? What is an example of your WORKING SAS code -- that correctly creates the table you want. Also, what is the explanation of your macro variables that you think you need?? It almost seems as though you plan to create a macro variable to hold each different comma that you need??? That really isn't necessary, macro logic or a macro %DO loop would allow you to determine when the end of the list of variables was reached so you could conditionally place the last comma.

One quick workaround, if you are always going to select the PROVFS variable would be to rework your select statement to put that variable last. All the SAS reporting procedures allow you to control the order of the variables you see, so order is not a major issue.:
[pre]
proc sql;
create table &title.1 as
select &base1.y1_f, &base1.y2_f, &base1.y3_f, &base1.y4_f,
&base2.y1_f, &base2.y2_f, &base2.y3_f, &base2.y4_f,
(&base1.y1_f+&base2.y1_f) as &title._sum1,
(&base1.y2_f+&base2.y2_f) as &title._sum2,
(&base1.y3_f+&base2.y3_f) as &title._sum3,
(&base1.y4_f+&base2.y4_f) as &title._sum4,
provfs
from _2010.facrep;
[/pre]

cynthia
Valued Guide
Posts: 632

Re: making variable lists in macros flexible to the number of parameters

After looking at your design as Cynthia suggests, if you still need a variable number of parameters you have a couple of options.
1) Use one parameter that contains a list that you then parse:
[pre]
%macro abc(baselist= basea baseb base3);
[/pre]
Parse this list with a %DO %WHILE and the %QSCAN function.

2) Use the PARMBUFF switch and then parse the automatic macro variable &SYSPBUFFR. This is discussed in section 13.4 of my macro book and Michael Mace wrote a 1999 paper for NESUG "Using &SYSBUFFR to Gather User Input". This is different from &SYSPARM, some folks get them confused.
Ask a Question
Discussion stats
  • 2 replies
  • 120 views
  • 0 likes
  • 3 in conversation