Discover More Tips and Techniques on This Blog

CALL SYMPUT vs CALL SYMPUTX

Call Symput:

Use CALL SYMPUT is you need to assign a data step value to a macro variable.

Syntax: Call Symput (“Macro variable”, character value)

The first argument to the Symput routine is the name of the macro variable to be assigned to the value from the second argument.

The second argument is the character value that will be assigned to the macro variable. The second argument need to be always a character value or if a numeric value is to be used it should convert first into character variable before assigning it to macro variable. It may lead to problems, if you don’t do the conversion from numeric to character. In this case, SAS automatically converts numeric value of the variable to character value before assigning it to macro variable and prints a message in the LOG saying that conversion happened.

See the example:

data _null_;
count=1978;
call symput('count',count);
run;
%put &count;

19 data _null_;
20 count=1978;
21 call symput('count',count);
22 run;

NOTE: Numeric values have been converted to character values at the places given by: (Line):(Column).
21:21
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.01 seconds


23 %put &count;
1978

Even though macro variable count is resolved to the value i.e 1978, SAS printed a note saying that NOTE: Numeric values have been converted to character values at the places given by: (Line):(Column). 21:21

To avoid that… you should do the conversion from numeric to character before assigning it to macro variable. Here is the syntax of that:

data _null_;
count=1978;
call symput('count',strip(put(count,8.)));
run;
%put &count;

29 data _null_;
30 count=1978;
31 call symput('count',left(put(count,8.)));
32 run;

NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds


33 %put &count;
1978

Note:

1) Even though we have created macro variable using the CALL SYMPUT but the same macro variable cannot be used in the same data step. The reason behind this is, macro code is compiled and executed before the data step code compiles and executes. So macro variable created by the CALL SYMPUT cannot available for the data step because macro variable compilation time occurs after or in the middle of the execution of the Data Step code.
(If a case arises where you have to access the same macro variable inside the data step, you can certainly do... by using two diff.. macro functions called RESOLVE or SYMGET)

2) SAS always aligns numeric values right and variable values get truncated as a part of this and to avoid that use the strip function to remove all the leading spaces as like in the above example.
3) If CALL SYMPUT is used outside the macro ( i.e open code) it creats global macro variable whereas it creats a local macro variable when it is used inside a macro.

CALL SYMPUTX:

SAS introduced CALL SYMPUTX in version 9 to address the pitfalls of CALL SYMPUT.

Advantages of CALL SYMPUTX over CALL SYMPUT include:

1) SYMPUTX automatically convert the numeric variables to character variables before assigning it to macro variable. (No need of manual conversion using PUT statement as given in the above example)

2) Call Symputx strips leading and trailing blanks. So no need of using STRIP or LEFT function to remove the leading spaces

Syntax: Call Symputx (“Macro Variable”, Character Value, Symbol Table)

First and second arguments are same as in CALL SYMPUT. The third argument (Symbol table) is optional and the valid value of it is G, L, and F. If we put G then the macro variable will be stored in the global symbol table, else if we specify L SAS will store the macro in the local symbol table, else if we don’t specify or specify F SAS follows the same rules as like for Call Symput.

Example:

data _null_;
count=1978;
call symputx('count',put(count,8.),’G’);
run;
%put &count;

29 data _null_;
30 count=1978;
31 call symputx('count',put(count,8.));
32 run;


NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds


33 %put &count;
1978

Note: If you use CALL SYMPUT instead of CALL SYMPUTX, the program executes in an identical manner, but a note is written to the SAS Log about conversion of numeric values to character values.


Here is the simple way to understand the diff between CALL SYMPUT AND CALL SYMPUTX:
Retrieved from: Using_the_SAS_V9_CALL_SYMPUTX_Routine from SAScommunity.org page:
Submitted by Michael A. Raithel.

The SAS V9 CALL SYMPUTX routine can save you keystrokes and lead to leaner, cleaner SAS programs.
Instead of using:
call symput('MACROVAR',trim(left(charvar)));

to load a SAS macro variable with a character string that might contain blanks, you could use SYMPUTX instead:
call symputX('MACROVAR',charvar);

5 comments:

  1. My cousin recommended this blog and she was totally right keep up the fantastic work!







    Data Conversion Company in Chennai

    ReplyDelete
  2. "Even though we have created macro variable using the CALL SYMPUT but the same macro variable cannot be used in the same data step. The reason behind this is, macro code is compiled and executed before the data step code compiles and executes. So macro variable created by the CALL SYMPUT cannot available for the data step because macro variable compilation time occurs after or in the middle of the execution of the Data Step code".........................this part is bit confusing. Can some tell what does this mean?

    ReplyDelete
  3. You cannot use a macro variable reference to retrieve the value of a macro variable in the same program (or step) in which SYMPUT creates that macro variable and assigns it a value.

    BECAUSE...

    ---> SYMPUT assigns the value of the macro variable during program execution.

    ---> But macro variable references resolve during the compilation of a step.

    ReplyDelete
  4. Very Informative!! in depth understanding.

    ReplyDelete

Disclosure:

In the spirit of transparency and innovation, I want to share that some of the content on this blog is generated with the assistance of ChatGPT, an AI language model developed by OpenAI. While I use this tool to help brainstorm ideas and draft content, every post is carefully reviewed, edited, and personalized by me to ensure it aligns with my voice, values, and the needs of my readers. My goal is to provide you with accurate, valuable, and engaging content, and I believe that using AI as a creative aid helps achieve that. If you have any questions or feedback about this approach, feel free to reach out. Your trust and satisfaction are my top priorities.