*---Macro %loopit iteratively loop through a list of variables and run PROC MEANS.

    You may replace PROC MEANS with other procedures;

 

data work.test;

  input id nv1 nv2 nv3 nv4;

  cards;

1  23 34 56 78

2  33 44 55 66

3  76 75 74 73

4  18 19 20 21

;

run;

 

*---Macro variable VARLIST hold a list of variables;

%let varlist = nv1 nv2 nv3 nv4 ;

 

%macro loopit( sds, varlist );

  %local i ;

  %let i = 1;

  %do %until (%scan(&varlist,&i,%str( )) = %str());

    %let var&i = %scan(&varlist,&i,%str( ));

    proc means data= &sds;

        title "Summary of &&var&i in &sds" ;

      var &&var&i;

    run;

      %let i = %eval(&i+1);

  %end;

  %let varcnt = %eval(&i-1);

  %put The number of variables being analyzed are &varcnt ;

%mend loopit;

 

*--Execute macro;

%loopit( work.test, &varlist ) ;