Macro variables with underscores

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 12
Accepted Solution

Macro variables with underscores

I am a beginner SAS coder and I have the following (excerpt) SAS macro:

 

 


%macro bts(input1, input2);

proc sql noprint;
select count(*) into :num_record
from b.&input1;
quit;
%put &num_record;


proc sql;
create table c.datafile as
select 
sqrt(&num_record)*(m_&input2/s_&input2+1/3*sum(x_&input2)/(&num_record*s_&input2**3)*(m_&input2/s_&input2)**2+1/(6*&num_record)*sum(x_&input2)/(&num_record*s_&input2**3)) as &input2

from

(select 
mean(&input2) as m_&input2, std(&input2) as s_&input2, (&input2-calculated m_&input2)**3 as x_&input2
from b.&input1);
quit;
%mend;

%bts(inv_per, cr_6m); 

The error that I get is:

 

 

 

WARNING: Apparent symbolic reference M_ not resolved.
&m_cr_6m

The variable cr_6m just consists of numbers and I think the reason I'm getting an error is because of the underscores. How can I fix this?

 


Accepted Solutions
Solution
‎03-26-2017 04:30 PM
PROC Star
Posts: 7,366

Re: Macro variables with underscores

Yes, that would mess things up! Try:

 

data somename;
set somename(rename=(
t_b_&input2.5=&input2
));
run;

Art, CEO, AnalystFinder.com

View solution in original post


All Replies
Super User
Super User
Posts: 6,502

Re: Macro variables with underscores

I cannot see how you would get that error message from the code you posted. Can you post the log instead?  You could turn on the MPRINT option to see what code the macro is generating.

 

It really looks like the error message is generated from code like this with a space after the underscore.

&m_ cr_6m

There is also a posibility that you are trying to generate macro variable name on the fly from other macro variables and the SAS parser is getting confused.  This can sometimes be solved by either deriving the name first before using it.

%let suffix = cr_6m ;
%let mvar= m_&suffix ;
%put value = &&mvar ;

Or by wrapping the expression in %UNQUOTE() function.

Occasional Contributor
Posts: 12

Re: Macro variables with underscores

Ahh, I think I realized my mistake, actually it was in another line:

 

 

data somename;
set somename(rename=(
t_b_&input25=&input2
));
run;

The actual name for the variable to be renamed is t_b_cr_6m5, but it's not recognizing the &input2 as cr_6m in the renaming command, how can I fix this?

 

Solution
‎03-26-2017 04:30 PM
PROC Star
Posts: 7,366

Re: Macro variables with underscores

Yes, that would mess things up! Try:

 

data somename;
set somename(rename=(
t_b_&input2.5=&input2
));
run;

Art, CEO, AnalystFinder.com

☑ This topic is solved.

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

Discussion stats
  • 3 replies
  • 310 views
  • 1 like
  • 3 in conversation