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

Hello,

I am trying to create a macro variable that will contain multiple variables in it.

I thought a macro array may be the answer but am not finding any documentation that seems to use a macro array in this way (unless I am misinterpreting the white papers I have been reading).

Here is a more detailed description of what I am trying to do.

1) I load a dataset in sas with multiple variables in it.

2) I am running multiple procselect statements to generate databases of randomly selected cases that meet a given set of criteria.  This set of criteria is defined by the variables in the dataset.

3) I am then running multiple logistic regression models using a different set of variables from the same dataset,

Because I am running multiple models using different variables in the proc select and the logistic regression I am having to change the by and var statements for the different procedures multiples times.

I am hoping I can set up a macro global variable (like the macro Let command) where I define all the variables (in one global variable) for a specific procedure at the beginning of the code and then just call that macro in all the proc select and logistic regression procedures.

Thank you for the help.

Cheers,

Scott

1 ACCEPTED SOLUTION

Accepted Solutions
Linlin
Lapis Lazuli | Level 10

Yes. If you want to do something similar to Art's example, you could put all your interested variables in one let statement:

%let vars=height weight;

proc sort data=sashelp.class out=want;

    by &vars.;

  run;

  proc print data=want;

    var &vars.;

  run;

View solution in original post

7 REPLIES 7
Linlin
Lapis Lazuli | Level 10

%let yourvariable=var1 var2 var3;

sbennet
Calcite | Level 5

Is it really that simple?

I didn't try that because in the %let help online help, it said you can only assign one value to a variable assigned by %let.

Thank you

Cheers,

Scott

Linlin
Lapis Lazuli | Level 10

Yes. If you want to do something similar to Art's example, you could put all your interested variables in one let statement:

%let vars=height weight;

proc sort data=sashelp.class out=want;

    by &vars.;

  run;

  proc print data=want;

    var &vars.;

  run;

sbennet
Calcite | Level 5

Thank you for the help!

art297
Opal | Level 21

In response to your question, yes it can be that simple.  The one value you are assigning happens to be a string that contains a list of variables.

However, given your description, I would use a macro rather than just a macro variable, passing your variables in the macro call.  e.g.,

%macro runit(vars);

  proc sort data=sashelp.class out=want;

    by &vars.;

  run;

  proc print data=want;

    var &vars.;

  run;

  proc means data=want;

    var &vars.;

  run;

%mend;

%runit(height weight)

%runit(age weight)

%runit(age height weight)

sbennet
Calcite | Level 5

Thank you for the help!

That would totally work.

I have to run multiple macros with similar variables so to make one variable statement that I can call for each of the different macro loops would work best for my current project.

DanielSantos
Barite | Level 11

I guess you found a simpler solution and you should stick to that.

But Macro Arrays are really just a designation, you can achieve that easily using a correct naming.

* one dim macro array, same thing for n dim macro array, just separate the dims with _;

%let X_0=2; * store here the max dim of the array;

%let X_1=HEIGHT; * cell 1;

%let X_2=WEIGHT; * cell 2;

then traversing the array is just a matter of using the right %do cycle:

%macro runit(MVAR_ARRAY);

proc sort data=sashelp.class out=want;

    by %do _IDX=1 %to &&&MVAR_ARRAY._0;

         &&&MVAR_ARRAY._&_IDX

         %end;

         ;

  run;

  proc print data=want;

    var %do _IDX=1 %to &&&MVAR_ARRAY._0;

        &&&MVAR_ARRAY._&_IDX

        %end;

        ;

  run;

%mend runit;

%runit(X);

Cheers from Portugal.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 7 replies
  • 23687 views
  • 3 likes
  • 4 in conversation