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 the small subtleties of their application.
For those who already work with this language enough, these things will seem elementary, but for beginners they will probably help to avoid the rake that 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 procedure symputis the name of the macro variable to which you need to assign the value of the second argument.

The second argument is the character value to be assigned to the macro variable. The second argument must always be exactly symbolic, otherwise, the numerical value must be converted to a symbol variable before assigning it to a macro variable. If you don’t type, this can lead to problems. In this case, SAS automatically converts the numerical value of the variable to a symbolic value before assigning it to a macro variable and displays a message in the log that the conversion has been performed.

Example: Although the value of the macro variable was defined as 1978, the SAS issued a remark saying
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


count
NOTE: Numeric values have been converted to character values at the places given by: (Line):(Column).
21:21


To avoid this, you should still convert the numerical value to a character value before assigning it to a macro variable. Here, for example, how to do this: Notes:

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



  1. Despite the fact that we created the variable with the help CALL SYMPUT, this macro variable cannot be used directly in the same datastep. The reason for this phenomenon is that macro code is compiled and executed before the datastep is compiled and executed. So, the macro variable being created CALL SYMPUTwill 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, of course, will be able to do this using two different macro functions - RESOLVE or SYMGET)
  2. SAS always aligns the numerical values ​​to the right, which leads to spaces at the beginning of the symbol variable during conversion, if the value of the 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 procedure is CALL SYMPUTused outside the macro (i.e. in open source), it creates a global macro variable, while when used inside a macro, it creates a local one.


CALL SYMPUTX


The procedure CALL SYMPUTXwas announced by SAS in version 9 in order to bypass traps CALL SYMPUT.
Advantages CALL SYMPUTXover CALL SYMPUTinclude:
  1. SYMPUTX Automatically convert numeric variables to character variables before assigning them to a macro variable. (You no longer need manual conversion using the PUT expression as in the above example)
  2. CALL SYMPUTXremoves leading and trailing spaces. So the need for functions, like STRIPor LEFT, to clean up extra spaces, is no longer needed .

Syntax:call symputx ("<макропеременная>", <символьное значение>, <таблица символов>)

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

Example: Note: If you used instead , the program would be executed identically, but a note would be issued to the log about the automatic conversion of a numerical value to a character value. A simple way to remember the difference between and :
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

CALL SYMPUTCALL SYMPUTX

CALL SYMPUTCALL SYMPUTX
(taken from Using_the_SAS_V9_CALL_SYMPUTX_Routine on SAScommunity.org)

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

Also popular now: