Passing a macro variable to a PROC?

Accepted Solution Solved
Reply
New Contributor
Posts: 4
Accepted Solution

Passing a macro variable to a PROC?

[ Edited ]

Just starting to learn using macros. I am using a macro from Example 1 on this SAS Reference page to create a macro variable which contains a list of all numeric variables in the dataset.

 

The idea is to then use this macro variable in PROC MEANS "VAR" statement, instead of hard-coding a specific list of numeric variables (i.e. hard-coding var1 and var2 when specifying "var1-numeric-var2").

 

I added a %put &varlist statement into the macro code to check that "varlist" does get populated with all numeric variables and outputs to the log. I then run the macro using %TEST_NUM. Immediately below that, I have the following code:

 

PROC MEANS data=xyz;
VAR &varlist;
run;

However this produces following errors:

 

WARNING: Apparent symbolic reference VARLIST not resolved.

ERROR 22-322: Syntax error, expecting one of the following: a name, ;, /, _ALL_, _CHARACTER_, _CHAR_, _NUMERIC_.  

ERROR 200-322: The symbol is not recognized and will be ignored.

ERROR: Variable VARLIST not found.

 

I am wondering if it is possible to pass a macro var into a PROC at all, or if it is, how could this be fixed. I also tried using a "%let varlist1 = &varlist; statement before the proc, and using &varlist1 in the proc, but same effect. 

 

Note: Oddly, I believe at some point I was able to get the PROC to run without error and produce the expected output when I tried to re-run a subset of this code, though was unable to replicate that on a rerun.

 

Ultimately, what I am trying to do is come up with an elegant way of running a program which contains several PROCs (PROC MEANS for all numeric vars, and PROC FREQ for all char vars) with different input datasets, by passing either all numeric, or all character vars into the respective PROC, without having to hard-code the vars in the VAR statements. Is that at all doable? Thank you.

 

 


Accepted Solutions
Solution
‎06-16-2016 11:34 AM
Super User
Super User
Posts: 7,994

Re: Passing a macro variable to a PROC?

Its hard to say from there, post the log with the information before that procedure and set:

options mlogic mprint symbolgen;

 

Which are the options you should use when debugging macros.

 

I would also point out that this coding is totally irrelevant, maybe they use it for teaching purposes, but in the real world:

proc means data=xyz;

  var _numeric_;

/* or  var var:; could also be used */

run;

View solution in original post


All Replies
Solution
‎06-16-2016 11:34 AM
Super User
Super User
Posts: 7,994

Re: Passing a macro variable to a PROC?

Its hard to say from there, post the log with the information before that procedure and set:

options mlogic mprint symbolgen;

 

Which are the options you should use when debugging macros.

 

I would also point out that this coding is totally irrelevant, maybe they use it for teaching purposes, but in the real world:

proc means data=xyz;

  var _numeric_;

/* or  var var:; could also be used */

run;

New Contributor
Posts: 4

Re: Passing a macro variable to a PROC?

Thank you, _numeric_ is what I was looking for. Learned something today!
☑ This topic is solved.

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

Discussion stats
  • 2 replies
  • 296 views
  • 1 like
  • 2 in conversation