sasmacrosdo-loops

In SAS, is there a way to run multiple proc freqs with different formats in a macro?


I want to run proc freqs with multiple tables for a large amount of variables, looking at each variable by demographics - gender, race, education, etc.. Instead of writing these by hand, I want to create a macro that will allow me to do this. The problem I have is that in the proc freq statement, each variable has a different format that I want to use, and I can't figure out how to accomplish this.

My code looks like this (with more variables):

%let variables = care hours primary;
%let variableforms = caref hoursf primaryf;

%macro mymacro;
%let i = 1;
%let var = %scan(&variables,&i);
%let varform = %scan(&variableforms, &i);

%do %while(%length(&var) > 0);

proc surveyfreq data=mydata;
tables &var;
tables gender*&var;
tables race*&var;
tables education*&var;
tables income*&var;
format &var &varform. gender genderf. race racef. education educationf. income incomef.;
run;

%let i = %eval(&i + 1);
%let var = %scan(&variables, &i);
%let varform = %scan(&variableforms, &i);
%end;
%mend;

%mymacro;

But I get warnings that the "variables" caref, hoursf, and primaryf aren't found in my data, and the formats are not applied. If I can't directly call the formats in a let statement, is there a way for me to do this?


Solution

  • Macro resolution gobbles up a trailing period, you'll want &varform...

    Otherwise the first iteration will result in format care caref gender genderf. [..], which will take the first three entries as variables to receive the format genderf..