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.

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
  • 7 replies
  • 21532 views
  • 3 likes
  • 4 in conversation