BookmarkSubscribeRSS Feed
amm8
Calcite | Level 5
First, I'm new to this forum and I'm not sure if this is the right group.

But, here's an example of what I'm trying to do.

I have a data set with 6 variables (group, variable_1-variable_5). Can I run a macro that would run the following for all variables but with only one call:

proc glm data=some_dataset;
class group;
model variable_x = group;
lsmeans group /pdiff adjust=bon;
run;

Can I make 1 call and the macro would be run 5 times? Another way, is there a way to run the macro for each variable (or subset) in a data set with a simple loop?

Thanks.
10 REPLIES 10
LinusH
Tourmaline | Level 20
Yes, you can use a %DO loop within a macro for this.
I'm not familiar with PROC GLM, but maybe there is a way to run the PROC just once for all variables, which might save some processing time if the source table is big. Maybe you have to transpose the table it first.

/Linus
Data never sleeps
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
Macro language basics use the %DO and %END to iterate, possibly adding a macro variable to increment with each loop - see simple code snippet below.

And from the SAS support http://support.sas.com/ website where I did a search on the argument "macro language iterative loop" and found the following technincal and SAS documentation references:

SAS® Macro Dynamics - From Simple Basics to Powerful Invocations
Rick Andrews, Centers for Medicare and Medicaid Services, Baltimore, MD
http://www2.sas.com/proceedings/sugi31/039-31.pdf

%DO, Iterative Statement
http://support.sas.com/documentation/cdl/en/mcrolref/61885/HTML/default/a000543755.htm

Scott Barry
SBBWorks, Inc.

%MACRO DOIT;
%PUT COUNT WITH ME: ;
%DO I=1 %TO 5;
%PUT &I;
%END;
%PUT DONE. ;
%MEND DOIT;
%DOIT;
amm8
Calcite | Level 5
Thanks for your responses. I think this could work if the variables were named x1, x2, x3, etc.

But, what if they are named height, width, volume, mass, etc. I'm looking for something that will work like proc means. If you specify variables, it will only run those variables. If you don't specify variables, it will run all variables in the dataset.

Is there an "internal" variable name in a dataset other than the name specified in the data statement that can be accessed? Perhaps it looks like x1, x2, x3, etc.

Thanks
LinusH
Tourmaline | Level 20
Do you really want to call your PROC with no regards to the column name? If no, you will have to specify somehow which columns you wish to use. If yes, this can be don in a number of ways.

You can have a data step reading SASHELP.VCOLUMN and do call execute to your macro with your proc.

The other way is to open your table using datastep functions, and loop trough the different variables. In this case they are called by position. This can be done within a data step or a macro.

/Linus
Data never sleeps
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
Here is a Google advanced search argument against SAS.COM website where you will find relative topic-oriented technical papers and documentation on the subject:

iterate dictionary columns generate code site:sas.com

Look at generating a list of macro variables and execute a general purpose SAS macro to invoke whatever code is necessary against each variable in the list, by referencing the macro variables.

Scott Barry
SBBWorks, Inc.
amm8
Calcite | Level 5
Thanks Linus,

Believe it or not, the answer is yes. I just received an Excel speadsheet that has 218 columns. The first column represents the group. They want the glm procedure with class=group on the 217 remaining variables.

I did it by brute force: a separate call for each variable. I figured there was an easier way to do this for the next time.

Ashy
data_null__
Jade | Level 19
In GLM you can use a list of variables or a "SAS Variable List" as the dependents in the model statement.

A "SAS Variable List" is a special way of referring to a list of variables. See docs for details but some are _CHAR_ _NUMERIC_ _ALL_ plus enumerate variable lists x1-x10 or variable range lists. You could use _NUMERIC_ of GROUP is a character or a range list as hight--mass as in the example you specified above.

Probably the easiset is to make GROUP character and specify

[pre]
class group;
model _numeric_ = group;
[/pre]

Look at the docs to learn the details of this very powerfull SAS feature. Message was edited by: data _null_;
amm8
Calcite | Level 5
Thanks data _null_;

That should do the trick. What they want may be wrong, but at least I won't get carpal tunnel by typing control-v 1000 times.
amm8
Calcite | Level 5
Thanks again,

This seems to work. The numbers are the same but the order of the output is a bit different. I don't mind. They have other problems to correct: i.e 217 variables and 300 records. Also, they can adapt to the new output order.
data_null__
Jade | Level 19
GLM will group the dependents by missing values. The maintain the order of the specified in the MODEL transpose the data and use a BY statement.

[pre]
dm 'clear log; clear output;';
data class;
set sashelp.class;
if _n_ in (2,3) then height=.;
run;
proc contents varnum short;
run;
proc glm;
class _character_;
model _numeric_ = sex;
run;



* To maintain variable order
1) transpose
2) establish variable order
3) sort
4) use BY statement in GLM
;
step1:
proc transpose data=class out=tall(rename=(col1=y));
by name sex; *unique ID for each row;
var _numeric_; *the default;
run;

step2_a:
proc transpose data=class(obs=0) out=vorder;
var _numeric_;
run;
step2_b:
data vorder(keep=_name_ vorder index=(_name_));
set vorder;
vorder + 1;
run;
step2_c:
data tall;
set tall;
set vorder key=_name_/unique;
run;
step3:
proc sort;
by vorder;
run;
proc print;
run;

step4:
proc glm;
by _name_ /*_label_*/ notsorted;
class sex;
model y = sex;
run;
[/pre]

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!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 10 replies
  • 1009 views
  • 0 likes
  • 4 in conversation