BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Tpham
Quartz | Level 8

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;

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

View solution in original post

5 REPLIES 5
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

Tpham
Quartz | Level 8

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;

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

Tpham
Quartz | Level 8

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?

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

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.

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
  • 5 replies
  • 2790 views
  • 0 likes
  • 2 in conversation