Trying to get a grasp of SAS arrays. I may have things all wrong.
Please see the following code:
array input_variables_array (*)
a b c d e f g h i j k l m n o p q r s t u v w x y z;
proc hplogistic data=sas_1.combined outest gconv=0;
model i_50505_Z_top20pct (event='1')
= input_variables_array;
run;
All variables are numeric. I created the array, right? Then I wanted to use it in the proc hplogistic, instead of writing out the whole alphabet again.
What am I doing wrong here?
Thanks!
Nicholas Kormanik
@NKormanik wrote:
@PaigeMiller wrote:
If the situation is more complicated, and you need to use the list of variables names in multiple places in your program, you can put the list of variables in a macro variable and then wherever you need this list, you use the name of the macro variable.
Yes, @PaigeMiller , that's the case. Given the above, how would that look using a macro approach?
The actual, as one would suppose, involves even more much longer named variables picked from throughout the dataset used several times within a program. Quite a large block to deal with and see repeatedly.
First, let's straighten out the terminology. I was not talking about "using a macro". I did suggest using a macro variable. Macro variable and macros are two different things. You should not call a macro variable by the name "macro", and vice versa.
@Tom shows how to use a macro variable. It's really very simple. If you have lots of variables, with long names, and there is something consistent about them (either common names, or consecutive in your data set), you can use SQL to create this macro variable.
Example to choose certain variable names not beginning with the letter S from data set SASHELP.CLASS:
proc sql noprint;
select name into :varlist separated by ' '
from dictionary.columns where name net 'S' and memname='CLASS' and libname='SASHELP';
quit;
%put &=varlist;
I created the array, right?
Well, no, you gave us a partial code without context, but it really doesn't matter because ARRAYs have no meaning to SAS PROCs. They are only useable in DATA steps.
If you are going to type out all variable names, do it in the PROC, not in a DATA step.
Or, you can always use valid SAS lists of variables, so if these variables are consecutive within a DATA set, you can use:
model i_50505_Z_top20pct (event='1') = a--z;
the double-dash indicating to use variables that are consecutive in the data set.
Or, if the situation is more complicated, and you need to use the list of variables names in multiple places in your program, you can put the list of variables in a macro variable and then wherever you need this list, you use the name of the macro variable.
@PaigeMiller wrote:
If the situation is more complicated, and you need to use the list of variables names in multiple places in your program, you can put the list of variables in a macro variable and then wherever you need this list, you use the name of the macro variable.
Yes, @PaigeMiller , that's the case. Given the above, how would that look using a macro approach?
The actual, as one would suppose, involves even more much longer named variables picked from throughout the dataset used several times within a program. Quite a large block to deal with and see repeatedly.
@NKormanik wrote:
@PaigeMiller wrote:
If the situation is more complicated, and you need to use the list of variables names in multiple places in your program, you can put the list of variables in a macro variable and then wherever you need this list, you use the name of the macro variable.
Yes, @PaigeMiller , that's the case. Given the above, how would that look using a macro approach?
The actual, as one would suppose, involves even more much longer named variables picked from throughout the dataset used several times within a program. Quite a large block to deal with and see repeatedly.
First, let's straighten out the terminology. I was not talking about "using a macro". I did suggest using a macro variable. Macro variable and macros are two different things. You should not call a macro variable by the name "macro", and vice versa.
@Tom shows how to use a macro variable. It's really very simple. If you have lots of variables, with long names, and there is something consistent about them (either common names, or consecutive in your data set), you can use SQL to create this macro variable.
Example to choose certain variable names not beginning with the letter S from data set SASHELP.CLASS:
proc sql noprint;
select name into :varlist separated by ' '
from dictionary.columns where name net 'S' and memname='CLASS' and libname='SASHELP';
quit;
%put &=varlist;
That is what the macro processor is for. Store the text you need to insert into your code into a macro variable. Then just expand the macro variable where you want that text inserted.
%let varlist=a b c d e f g h i j k l m n o p q r s t u v w x y z;
proc hplogistic data=sas_1.combined outest gconv=0;
model i_50505_Z_top20pct (event='1') = &varlist. ;
run;
@Tom That considerably cleans thing up. Thanks a bunch!
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.