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

I am working on a model that performs regression analysis using the PROC GLMSELECT statement. In order to make my program as dynamic as possible I would like to assign all of the numeric variables from my source table to a macro variable that can be referenced in the model statement, thus allowing the name and number of variables in the input table to change without the need to change the regression code. I would also like to create a macro variable for the categorical variables to be referenced in the Class statement. Please help!

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

There are a few ways to get all variables.  The one I would recommend for your purposes is PROC CONTENTS (followed by PROC SQL), because you might want to exclude the dependent from your list of variables.  For example, you could code:

 

proc contents data=have (drop=&dependent)  noprint out=_contents_ (keep=name type);

run;

 

That gives you a SAS data set with the name and type of all variables in your data set, excluding the dependent.

 

Then SQL takes over:

 

proc sql;

select strip(name) into : numvars separated by  ' '  from _contents_ 

where type=1;

select strip(name) into : charvars separated by ' ' from _contents_

where type=2;

quit;

 

 

View solution in original post

3 REPLIES 3
Astounding
PROC Star

There are a few ways to get all variables.  The one I would recommend for your purposes is PROC CONTENTS (followed by PROC SQL), because you might want to exclude the dependent from your list of variables.  For example, you could code:

 

proc contents data=have (drop=&dependent)  noprint out=_contents_ (keep=name type);

run;

 

That gives you a SAS data set with the name and type of all variables in your data set, excluding the dependent.

 

Then SQL takes over:

 

proc sql;

select strip(name) into : numvars separated by  ' '  from _contents_ 

where type=1;

select strip(name) into : charvars separated by ' ' from _contents_

where type=2;

quit;

 

 

ballardw
Super User

@tgrandchamp wrote:

I am working on a model that performs regression analysis using the PROC GLMSELECT statement. In order to make my program as dynamic as possible I would like to assign all of the numeric variables from my source table to a macro variable that can be referenced in the model statement, thus allowing the name and number of variables in the input table to change without the need to change the regression code. I would also like to create a macro variable for the categorical variables to be referenced in the Class statement. Please help!


Only you will know which variables are categorical unless you can absolutely positively never ever fails a variable is categorical if there are fewer than XX unique values. If that is the case then Proc freq with nlevels option may be helpful such as:

ods select freq.nlevels;
proc freq data=sashelp.class nlevels;
ods output nlevels = work.mylevels;
run;

which will build a data set with variable and number of unique values, missing and levels of missing. The something like this,

 

Proc sql noprint;
   select tablevar into : catvars separated by ' '
   from work.mylevels
   where Nlevels le 6;
quit;
%put &catvars;

works assuming the XX I mentioned above is 6.

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

What is ANOVA?

ANOVA, or Analysis Of Variance, is used to compare the averages or means of two or more populations to better understand how they differ. Watch this tutorial for more.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 3277 views
  • 2 likes
  • 4 in conversation