BookmarkSubscribeRSS Feed
James18
Obsidian | Level 7

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!

 
5 REPLIES 5
PaigeMiller
Diamond | Level 26

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
Tom
Super User Tom
Super User

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;
Reeza
Super User

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);

 

dwrightii
Fluorite | Level 6

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.

Dennis Wright, II
muawia27
Calcite | Level 5
10 + 20 is the answer

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 5 replies
  • 864 views
  • 0 likes
  • 6 in conversation