Help using Base SAS procedures

Array outside the data statement?

Accepted Solution Solved
Reply
Contributor
Posts: 58
Accepted Solution

Array outside the data statement?

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;


Accepted Solutions
Solution
‎11-18-2014 11:14 AM
Super User
Super User
Posts: 7,942

Re: Array outside the data statement?

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


All Replies
Solution
‎11-18-2014 11:14 AM
Super User
Super User
Posts: 7,942

Re: Array outside the data statement?

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.

Contributor
Posts: 58

Re: Array outside the data statement?

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;

Super User
Super User
Posts: 7,942

Re: Array outside the data statement?

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.

Contributor
Posts: 58

Re: Array outside the data statement?

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?

Super User
Super User
Posts: 7,942

Re: Array outside the data statement?

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.

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 231 views
  • 0 likes
  • 2 in conversation