So is there a way to make an array work outside of the data statement?
So basically I have to run this:
I have to run the following macro for 200 variables:
%macro %somemacro(var);
proc sgpanel data=dataset;
panelby visit/rows=1 columns=3 spacing=5 novarname;
vbox &var / category=subgrp;
rowaxis values=(0 to 100 by 20);
run;
%mend somemacro;
The variables are not in numerical order so I can't do a regular do loop. For example a few variables: race, aduPH, BMI)...
I thought an array would be helpful for this situation but I I think it is only for the data statement. SO I am wondering if there is a more efficient solution?
This is what I have tried:
array adu{*} race bmi aduPH aduFEEL aduVIEW aduSPORT;
do i=1 to dim(adu);
%somemacro(adu(i));
end;
Hi,
Lots of options there.
data _null_;
do I="race","bmi","aduPH"....;
call execute('%someone ('||strip(I)||');');
end;
run;
More importantly, if you are able to do the same thing on all those variables, why would you need a macro at all?
data _null_;
call execute('VLIST',"race bmi aduph...");
run;
data want;
array adu{*} &vlist.;
...
Many ways to achieve the same thing, but need to know inputs/outputs.
Hi,
Lots of options there.
data _null_;
do I="race","bmi","aduPH"....;
call execute('%someone ('||strip(I)||');');
end;
run;
More importantly, if you are able to do the same thing on all those variables, why would you need a macro at all?
data _null_;
call execute('VLIST',"race bmi aduph...");
run;
data want;
array adu{*} &vlist.;
...
Many ways to achieve the same thing, but need to know inputs/outputs.
Basically I am running 200 boxplots.. So I have all the variable name in a list, since I added them in the keep statement in my dataset)
this is the macro:
%macro %somemacro(var);
proc sgpanel data=dataset;
panelby visit/rows=1 columns=3 spacing=5 novarname;
vbox &var / category=subgrp;
rowaxis values=(0 to 100 by 20);
run;
%mend somemacro;
Ok, so assuming WORK.TEST holds each of those variables you want to run box plots on:
data _null_;
set sashelp.vcolumns (where=(libname="WORK" and memname="TEST"));
call execute('proc sgpanel data=work.test;
panelby visit/rows=1 columns=3 spacing=5 novarname;
vbox '||name||' / category=subgrp;
rowaxis values=(0 to 100 by 20);
run;');
run;
This will generate a proc sgpanel for each variable in work.test.
Thanks for this.. so with this, I can change the libname and the memname with my active dataset right?
And with the call execute, will this run the sgpanel for all the variables in the dataset, since there is no "list" in dataset?
Yep. As an explanation, the data _null_ is just a datastep. It reads from the SAS metadata tables, in this case column deifnitions ones, taking data with libname of WORK and dataset name of TEST. This returns a set of rows which detail all the variables in that dataset. Then for each row (as a datastep is a loop in itself), it generates a SAS code step of the proc sgpanel, replacing the ||name|| (well concatenating the strings) with the variable name in the current row of execution. Hence the proc sgpanel gets replicated for every row returned from the where clause.
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.