BookmarkSubscribeRSS Feed
mariko5797
Pyrite | Level 9

I created a basic macro with SAS procedures to run through PROC MEANS and assign formats using an array. See a very rough outline below.

%macro(var, fmt1, fmt2);
   proc means data = have;
     class BMI;
     var &var;
     output out = stats_&var;
  run;

  data stats_&var;
     set stats_&var;
     array.......
  run;
  proc sort...; run;
  proc transpose data = stats_&var out = stats2_&var;
    by name;
    var value;
    id BMI;
  run;
%mend

I would like to run this macro repeated for a set of variables. Rather than filling in the macro multiple times, I want to run it through a do loop. I have a vague memory of doing this in the past. Something like this:

%macro loop(variables);
  %scan();
     %stats(var = &variables, fmt1 = f5.2, fmt2 = f6.1);
%mend;

%loop (WGT, HGT, BMI); /*variables weight, height, and bmi will run through the macro*/

I'm not familiar with loops, so I'm not sure all the macro functions required to run. Can someone help? Thank you in advance.

4 REPLIES 4
Tom
Super User Tom
Super User

Your proposed solution seems suboptimal.  If you are going to generate the same statistics from PROC MEANS then generate them for ALL of the variable of interest at once.

 

But to your question: to iterate over a list just use a DO loop.

%macro loop(variables);
%local i var;
%do i=1 %to %sysfunc(countw(&variables,%str( )));
  %let var=%scan(&variables,&i,%str( ));
  %stats(var = &var, fmt1 = f5.2, fmt2 = f6.1)
%end;
%mend;
%loop(variables=WGT HGT BMI)
mariko5797
Pyrite | Level 9

My concern doing all variables in PROC MEANS is that not all the variables require the same formatting for the same statistic. There would be a lot of columns to assign a format to.

PaigeMiller
Diamond | Level 26

Your example assigns the same format to every call of the macro, and then that macro doesn't even use those formats.

 

It's seems to me that doing this via a macro is not more efficient, or any less typing, than doing it without a macro and assigning formats in a FORMAT statement. One way or another, you will have to type out the formats, having a macro does not eliminate the need to specify formats. And if you do this without a macro, and call one PROC MEANS that works on all of your variables in &VARIABLE, this is much more efficient in terms of run time (and in terms of programming) than a macro that calls PROC MEANS once for each variable.

 

And lastly, without a macro, it is much easier to program!

 

So, if it was me, I would forget the macro idea for this call to PROC MEANS

--
Paige Miller
ballardw
Super User

Large economy sized hint: Provide an example data in the form of data step code or use one of the SAS supplied data sets like SASHELP.CLASS or SASHELP.CARS and show what you expect.

 

Are you sure that you need a data set at all? The sort of statistics you are requesting (by not listing any) are available in the reporting procedures Tabulate and Report.

Examine this data set output:

proc means data=sashelp.class;
   class sex;
   var height weight;
   output out=stats;
run;

And similar output from proc tabulate but specifying different formats for the same statistic for different variables

proc tabulate data=sashelp.class;
   class sex;
   var height weight;
   tables height *(n mean*f=8.3  max*f=6.1 min*f=best5. std*f=8.5)
          weight *(n mean*f=9.5  max*f=4.0 min*f=best3. std*f=12.8)
          , All='All' Sex
   ;
run;

Also, Proc tabulate can have multiple table statements.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 1066 views
  • 1 like
  • 4 in conversation