📜 ⬆️ ⬇️

CALL SYMPUT vs CALL SYMPUTX or SAS Base for Dummies

Good afternoon, dear readers.
In this article I will talk about two procedures of the SAS Base language and about the small subtleties of their use.
To those who are already working enough with this language, these things will seem elementary, but for beginners, they will probably avoid the rake, which I stepped on at the very beginning of my acquaintance with SAS Base.

CALL SYMPUT


Call Symput is used for cases when the value of a variable in a data step (datastep) needs to be assigned to a macro variable.
Syntax: call symput ("<>", < >)

The first argument in the symput procedure is the name of the macro variable, which must be assigned the value of the second argument.

The second argument is a character value that will be assigned to the macro variable. The second argument must always be character, otherwise, the numeric value must be converted to a character variable before it is assigned to a macro variable. If you do not give the type, it can turn into problems. In this case, SAS automatically converts the numerical value of the variable into a symbolic value before assigning it to a macro variable, and displays in the log a message that the conversion was made.
')
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


Although the value of the macro variable count was defined as 1978, SAS issued a remark saying
NOTE: Numeric values have been converted to character values at the places given by: (Line):(Column).
21:21


To avoid this, you still need to convert the numeric value into a symbolic value before assigning it to a macro variable. Here, for example, how this can be done:

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


Notes:
  1. Despite the fact that we created a variable using CALL SYMPUT , this macro variable can not be used directly in the same datastep. The reason for this phenomenon is that the macro code is compiled and executed before the datastep is compiled and executed. So, the macro variable created by CALL SYMPUT will not be available in the datastep due to the fact that this macro variable will be created and assigned only after or during the execution of the datastep code.
    (In case you still need access to the same macro variable inside the datastep, you will, of course, be able to do this with two different macro functions - RESOLVE or SYMGET )
  2. SAS always aligns numerical values ​​on the right edge, which leads to spaces at the beginning of a character variable during conversion, if the value of a numerical variable was less than its length. To get rid of these spaces, use functions that remove all leading spaces (as in the example above).
  3. If the CALL SYMPUT procedure is used outside of a macro (that is, in open source), it creates a global macro variable, whereas when used inside a macro, it creates a local one.


CALL SYMPUTX


The CALL SYMPUTX was announced by SAS in version 9 in order to bypass the CALL SYMPUT traps.
The advantages of CALL SYMPUTX over CALL SYMPUT include:
  1. SYMPUTX automatically converts numeric variables to character variables before assigning them to a macro variable. (You no longer need to manually convert with the aid of PUT as in the example above)
  2. CALL SYMPUTX removes leading and trailing spaces. So the need for functions, like STRIP or LEFT , to clear extra spaces, is no longer necessary.

Syntax: call symputx ("<>", < >, < >)

The first two arguments are the same as the CALL SYMPUT arguments. The third argument (symbol table) is optional and can be G, L or F. If you specify G, a macro variable will be created in the global symbol table, but if you specify L, SAS saves the macro variable in the local table. If the third argument is not specified or set to F, CALL SYMPUTX will behave similarly to CALL SYMPUT .

Example:
data _null_;
count=1978;
call symputx('count',count,'G');
run;
%put &count;

29 data _null_;
30 count=1978;
31 call symputx('count',count,'G');
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 used CALL SYMPUT instead of CALL SYMPUTX , the program would run identically, but a note would be issued to the log about automatic conversion of a numerical value to a character value.

A simple way to remember the difference between CALL SYMPUT and CALL SYMPUTX :
(taken from the Using_the_SAS_V9_CALL_SYMPUTX_Routine page on SAScommunity.org)

The SAS V9 CALL SYMPUTX procedure allows you to save keystrokes and create a more compact and clear code.
Instead of using
call symput('macrovar', trim(left(charvar)));
To create a macro variable with a character string that may contain extra spaces, you should use SYMPUTX:
call symputx('macrovar', charvar);

Source: https://habr.com/ru/post/142147/


All Articles