- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
I am having some trouble grasping the full understanding of how a macro variables function
Suppose the following macro variable was submitted:
%LET NewVbl = 10 + 20;
Would the value of the NewVbl be 30?
I understand how macro variables function if they are a set value (such as %Let NewVbl = 30), so what would be the purpose of adding a calculation with a macro variable?
Any information would be greatly appreciated!
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Suppose the following macro variable was submitted:
%LET NewVbl = 10 + 20;
Would the value of the NewVbl be 30?
No, it is not 30, you can confirm this by running the code yourself
%let newvbl = 10 + 20;
%put &=newvbl;
Remember the macro processor processes strings of text. So when it sees
10 + 20
it thinks this is a string of 7 characters, and it does not think to perform arithmetic on it.
If you want to do arithmetic in the macro processor, this will allow the macro processor to perform INTEGER arithmetic
%let newvbl = %eval(10 + 20);
%put &=newvbl;
Paige Miller
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
To you first question, no. The macro variable will literally have the text 10 + 20.
If you did want to calculate that sum you could use the %EVAL() macro function.
%LET NewVbl = %eval(10 + 20);
But note that %EVAL() only does integer arithmetic. If you need to perform floating point operations (usually comparison rather than actual operations) then %SYSEVALF() function instead.
The reason to perform the sum in macro code is if you need to use the string 30 to generate some code. For example if 30 needs to be used as part of a variable name. So you could use :
proc freq ;
tables var%eval(10 + 20) ;
run;
To generate this code:
proc freq ;
tables var30 ;
run;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Would the value of the NewVbl be 30?
No. The value is the text value of "10 + 20".
SAS will treat it as a default text character until you either try and resolve it. If you want the value of 30 stored inside, you could wrap it with %EVAL() or %SYSEVALF which allow you to exercise functions within the macro language.
Purpose for adding a calculation can differ, you don't usually see it as shown because like you've mentioned that's very simple. But what you'll often see are references to other variables or macro variables that could have different dynamic values. Or for complicated formulas that need to be used multiple places but you want to ensure that you're typing it correctly and any changes happen in all locations.
@James18 wrote:
I am having some trouble grasping the full understanding of how a macro variables function
Suppose the following macro variable was submitted:
%LET NewVbl = 10 + 20;
Would the value of the NewVbl be 30?
I understand how macro variables function if they are a set value (such as %Let NewVbl = 30), so what would be the purpose of adding a calculation with a macro variable?
Any information would be greatly appreciated!
%let newVal = %eval(30 + &oldVal);
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hi there,
One aspect we can also watch out for is the observations in our data set. If, for example, none of the observations meet the criteria of your macro, then there will be nothing to output. I recommend (as a test) changing one of the numbers in your arithmetic to where you can be sure the result is in the data set, and try it. If it works, then the initial arithmetic may have worked exactly as designed.
For example, using your description below, say that your variable's values ranged from 40 to 90. If you set your macro to return observation values below 30, it might look like the code didn't run, but it did, and the output would be blank.
Check your log - it may be that the macro ran without errors, and your value of 30 is reported there, despite the lack of output.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content