0
votes

I can't seem to find information about this online...

I have a list of variables I want to do a proc summary on. As these proc summaries are performed individually per variable, it would be faster for me if I can find some way to loop through a numbered list of variables, then create an output to excel or simply a combined table of results that clearly indicates what results belong to what variable.

The problem is I only know do loops work in a datastep, how would I get this to work for proc steps? Could I write a macro for the proc step, then nest it within a datastep? Would this cause it to run appropriately? i.e.

data _NULL_;
 set table_of_vars;

do i=1 to (number of vars in the table);

_n_ = i; 

%let var = _n_;
%macro_proc_summ(&var.);

end;

and another code subsequently that merges the individual output, or perhaps the macro could even generate output that always appends information.

Obviously the code is very sketchy, but conceptually could this work?

EDIT: To give a bit more clarity, this is how the code would look like without a loop in place.

%macro Analysis(var); %macro _; %mend _;
proc summary data=masterdata nway missing; 
class &var.;
output out = &var._summ (drop = _type_);
run;
%mend;
endrsubmit;

%Analysis(var1);
%Analysis(var2);
%Analysis(var3);
.
.
.
.   
%Analysis(var100);

From here we could either:

  • Export var1_summ, var2_summ to Excel in cells A1, D1, etc.
  • Or first combine our individual summaries into a large table then export to some graphing application to look at the trend.

Either way you can see how these are individual Proc steps, which could be done a lot quicker in a loop.

1
You can pass multiple variables to proc summary at once and generate a combined output. You don't need a macro for this at all.Reeza
@Reeza - Not in my case I think? I want to "class" a bunch of variables and when done in a single step, the output does not combine the levels within the variable uniquely... For example, Var1 could be Shoes, and Var2 could be Shirt, and they would If I list them all in one summary it would merge the information oddly...Wolfspirit
How will you deal with numeric and characters variables? You'll have to process them somehow so you can stack them. It sounds like you want Table 1 for reporting in papers - general summary? If so, you should search on lexjansen.com there's ton of examples. Otherwise though, loop through the values using call execute instead. Especially if the parameters are in your dataset.Reeza
@Reeza Yes - Table1 would be a "one-way" summary table, after which I will graph the frequencies of each level to show its corresponding exposure. You are also right - Merging the results would mean I need an additional column saying that the variable level belongs to a particular summarised column. Maybe I'm searching for the wrong thing - I'll check out your link for now.Wolfspirit

1 Answers

0
votes

If you don't want an output table use Proc Tabulate.

proc tabulate data=sashelp.class out=summary1;
class sex age;
var weight;
table sex age, weight*(n mean min max median);
run;

data summary2;
set summary1;
Var=coalescec(sex, put(age, 2.));
drop age sex _:;
run;

EDIT: Rather than proc tabulate, if you only want N, use PROC FREQ

*Run frequency for tables;
ods table onewayfreqs=temp;
proc freq data=sashelp.class;
    table sex age;
run;

*Format output;
data want;
length variable $32. variable_value $50.;
set temp;
Variable=scan(table, 2);

Variable_Value=strip(trim(vvaluex(variable)));

keep variable variable_value frequency percent cum:;
label variable='Variable' 
    variable_value='Variable Value';
run;

*Display;
proc print data=want(obs=20) label;
run;