BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
GN0001
Barite | Level 11

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

Blue Blue
1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

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

--
Paige Miller

View solution in original post

7 REPLIES 7
Kurt_Bremser
Super User

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.

 

PaigeMiller
Diamond | Level 26

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

--
Paige Miller
GN0001
Barite | Level 11
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
Blue Blue
Tom
Super User Tom
Super User

@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.

 

Tom
Super User Tom
Super User

Read the documentation first. https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.2/lefunctionsref/p1fa0ay5pzr9yun1mvqxv8ipzd4d.h...

 

Spoiler

Syntax

CALL SYMPUTX(macro-variable, value <, symbol-table>);

 

 

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:

 

  1. Macro variable X is set to the string 1
  2. Macro variable ITEMS is set to the string 2
  3. The macro %TEST() is called passing 100 as the single parameter value.
  4. It writes a string that includes the value of the ITEMS macro variable.
  5. It writes a string that includes the value of the X macro variable.

Now let's ask some questions.

What are the differences between the two CALL SYMPUX() function calls?

 

Spoiler
They are writing to two different macro variables, ITEMS and X.
The first one adds the optional third argument 'L' which means to store the macro variable in the LOCAL symbol table 
The second one just uses the default setting for the third argument.

What impact to you expect that to have on the results shown by the last two %PUT statements?

 

 

Spoiler
The value of ITEMS has not changes, so it is still 2, but the value of X has been replaced with the string 123.456

Run it.  Did it produce the output you expected?

 

Spoiler
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!

 

 

GN0001
Barite | Level 11
%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?

Blue Blue
Kurt_Bremser
Super User

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.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

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!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 7 replies
  • 1609 views
  • 5 likes
  • 4 in conversation