DATA Step, Macro, Functions and more

Create a macro variable with multiple variables in it

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 15
Accepted Solution

Create a macro variable with multiple variables in it

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


Accepted Solutions
Solution
‎08-10-2012 11:38 AM
Super Contributor
Posts: 1,636

Re: Create a macro variable with multiple variables in it

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


All Replies
Super Contributor
Posts: 1,636

Re: Create a macro variable with multiple variables in it

%let yourvariable=var1 var2 var3;

Occasional Contributor
Posts: 15

Re: Create a macro variable with multiple variables in it

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

Solution
‎08-10-2012 11:38 AM
Super Contributor
Posts: 1,636

Re: Create a macro variable with multiple variables in it

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;

Occasional Contributor
Posts: 15

Re: Create a macro variable with multiple variables in it

Thank you for the help!

PROC Star
Posts: 7,468

Re: Create a macro variable with multiple variables in it

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)

Occasional Contributor
Posts: 15

Re: Create a macro variable with multiple variables in it

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.

Super Contributor
Posts: 474

Re: Create a macro variable with multiple variables in it

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.

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

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