0
votes

I'd like to improve a SAS macro that loops through some variables and prints frequency tables for them.

Here's the working macro:

%macro iraCatFreq(IRACatsList);
%local i next;
%let i=1;
%do i=1 %to %sysfunc(countw(&IRACatsList));
    %let next=%scan(&IRACatsList,&i);
    proc freq data=out.SCF16_Cath;
    weight WGT;
    by ANYPEN;
    tables &next;
    format rRothContrBin allBinaries_format.; 
    run;
%end;
%mend iraCatFreq;
%iraCatFrew(rRothContrBin sRothContrBin oRothContrBin rRegContrBin sRegContrBin oRegContrBin);

I wanted to improve this macro by replacing the parameter list of variable names (rRothContrBin sRothContrBin oRothContrBin rRegContrBin sRegContrBin oRegContrBin) with a single parameter.

Here's the approach I cobbled together:

/*Create a table of variable names for IRA types and owners*/

data IRACats;
    infile datalines;
    length x $ 17;
    input x $;
    datalines;
rRothContrBin
sRothContrBin
oRothContrBin
rRegContrBin
sRegContrBin
oRegContrBin
    ;

/*Convert table of strings into a macro variable, &IRACatsList */

proc sql noprint;
    select x into :IRACatsList separated by ' '
    from IRACats;
quit;
%put IRACatsList = &IRACatsList;

I tried running the macro after running this approach,

%iraCatFreq(&IRACatsList);

It works, but is there a better way to do this without the data and proc sql steps?

1
Is there any reason you aren't just putting all those variables into a single tables statement within proc freq?user667489
I don't understand the question. Your macro is already taking in a single parameter that accepts a list of values. What is it that you want to change?Tom
Uh... I forgot I could.AwfulPersimmon

1 Answers

2
votes

You can call the existing macro with a single macro variable and without using proc sql or data steps like this:

%let IRACatsList = rRothContrBin sRothContrBin oRothContrBin rRegContrBin sRegContrBin oRegContrBin;
%iraCatFreq(&IRACatsList);