Hello team,
%let x=1; %let items=2; %macro test(val); data _null_; call symputx('items', ' leading and trailing blanks removed ', 'L'); call symputx(' x ', 123.456); run; %put local items &items; %mend test; %test(100) %put items=!&items!; %put x=!&x!;
Can someone please explain about this?
call symputx('items', ' leading and trailing blanks removed ', 'L');
What does this three strings passed to symputx mean?
I look at the log, I am totally confused.
First, it says these two macro variables:
%let x=1; %let items=2;
and then it says:
call symputx('items', ' leading and trailing blanks removed ', 'L'); call symputx(' x ', 123.456);
what does %test(100)?
Regards,
BittenApple
call symputx assigns the text string 'leading and trailing blanks removed' to macro variable &ITEMS, and the 'L' makes it a %LOCAL macro variable. Local means that the value of the macro variable is only available inside the macro that created it.
Outside the macro that created it, the value inside the macro is not available and so when you try to use &ITEMS outside the macro, it has a different value. I leave it as a homework assignment for you to figure out why it has the value 2 outside the macro, and why the %put items=!&items!; produces the text string with a 2.
%test(100) executes the macro with argument 100
The code demonstrates the effect and use of global vs. local symbol tables.
First, two macro variables are defined in the global scope.
Then, a macro is defined in which a DATA step is used to call CALL SYMPUTX twice; once with the 'L' parameter, which forces the creation of a new variable with the same name in the local symbol table, and second without this parameter, which means that the global macro variable (if it already exists) will be reset with the new value.
Finally, the macro is called, and the final outcome of both macro variables is displayed. Study the log closely.
For confusion, a macro parameter (val) is defined but never used.
call symputx assigns the text string 'leading and trailing blanks removed' to macro variable &ITEMS, and the 'L' makes it a %LOCAL macro variable. Local means that the value of the macro variable is only available inside the macro that created it.
Outside the macro that created it, the value inside the macro is not available and so when you try to use &ITEMS outside the macro, it has a different value. I leave it as a homework assignment for you to figure out why it has the value 2 outside the macro, and why the %put items=!&items!; produces the text string with a 2.
%test(100) executes the macro with argument 100
@GN0001 wrote:
Hello PaigeMiller,
Because 2 is the local and it is restricted inside the function symputx. But x is not local and so its values is overwritten by 123.456.
I think it should be the other way around, I mean the value of x is 123.456 should be overwritten by value X=1.
Regards,
BittenApple
Unlike in FORTRAN in SAS macro code %LOCAL does not mean that the macro variables are restricted to just that macro. Instead all active macro variables are available to be used and modified (except those hidden by the existence of a macro variable with the same name in a more inner scope). You don't use %LOCAL to protect your macro variables from other macros. You use them to prevent your macro from causing trouble for the environments that called your macro.
Normally you cannot define a macro as GLOBAL if there is already one with that name that is LOCAL. (Why would you since you could never assign it a value?) But the "G" option of CALL SYMPUTX() allows you to by pass that restriction and push a value directly into the GLOBAL symbol table, even when there is already a LOCAL macro variable with the same name. Of course you still cannot see the value while inside the current macro (unless you use a trick like this %symget() function style macro ). But it is useful for returning values from a macro call so that the values live on after the macro call ends.
Read the documentation first. https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.2/lefunctionsref/p1fa0ay5pzr9yun1mvqxv8ipzd4d.h...
Let's re-order the statements in that code to make it more logical.
So first define the macro:
%macro test(val);
data _null_;
call symputx('items', ' leading and trailing blanks removed ', 'L');
call symputx(' x ', 123.456);
run;
%put local items &items;
%mend test;
So when you call that macro it runs a data step and then issues a macro %PUT statement to write something to the SAS log.
Now let's look at the executable part of the code:
%let x=1;
%let items=2;
%test(100)
%put items=!&items!;
%put x=!&x!;
Looking at that code here is what the statements mean:
Now let's ask some questions.
What are the differences between the two CALL SYMPUX() function calls?
What impact to you expect that to have on the results shown by the last two %PUT statements?
Run it. Did it produce the output you expected?
2263 %let x=1; 2264 %let items=2; 2265 %test(100) NOTE: DATA statement used (Total process time): real time 0.00 seconds cpu time 0.00 seconds local items leading and trailing blanks removed 2266 %put items=!&items!; items=!2! 2267 %put x=!&x!; x=!123.456!
%macro test(val); data _null_; call symputx('items', ' leading and trailing blanks removed ', 'L'); call symputx(' x ', 123.456); run; %put local items &items; %mend test;
Why does it say items &items? Why does the first item stand for?
%let x=1; %let items=2; %test(100) %put items=!&items!; %put x=!&x!;
How do we know that 1 refers to first position of the string?
First question:
%put local items &items;
"local items" is just text which is literally written to the log. &items is a macro variable reference; if the variable exists, its content is placed in place of the reference.
Second question:
"1" is no position, it is just text stored in a macro variable.
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
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.
Ready to level-up your skills? Choose your own adventure.