SAS: CALL SYMPUT and CALL SYMPUTX

Deepanshu Bhalla Add Comment

This tutorial explains how to use CALL SYMPUT and CALL SYMPUTX in SAS, along with examples.

SAS: CALL SYMPUT and CALL SYMPUTX

CALL SYMPUT

In SAS, the CALL SYMPUT routine is used to create macro variables. It assign a value of the data step to a macro variable.

Below is the syntax of CALL SYMPUT :

CALL SYMPUT (macro-variable, value);
  • macro-variable is the name of the macro variable you want to create.
  • value is the value you want to assign to the macro variable.
Simple Example of CALL SYMPUT

In the SAS code below, a data step is used to assign a value of '25' to the macro variable myvalue using the CALL SYMPUT routine.

data _null_;
call symput('myvalue', '25');
run;
%put value = &myvalue.;
Result
 value = 25

The data _null_; statement initiates a data step that does not create an output dataset.

The %put statement is used to display the value of the macro variable in the SAS log.

Assign Current Date to Macro Variable

In the code below, we are assigning the current date to the macro variable mydate using the CALL SYMPUT routine.

data _null_;
call symput('mydate', PUT(TODAY(), DATE11.));
run;
%put &mydate.;
Result
30-JUN-2023

The CALL SYMPUT assigned the value returned by PUT(TODAY(), DATE11.) to the macro variable mydate. The PUT function is used to convert the current date TODAY() into the DATE11. format, which displays the date as dd-mmm-yyyy (e.g., 30-JUN-2023).

CALL SYMPUTX

The CALL SYMPUTX routine assign a value of the data step to a macro variable and removes both leading and trailing spaces.

Here is the syntax of CALL SYMPUTX :

CALL SYMPUTX(macro-variable, value, symbol-table);
  • macro-variable is the name of the macro variable you want to create.
  • value is the value you want to assign to the macro variable.
  • symbol-table Refer the values of symbol-table below.
    • G: Macro variable is stored in the global symbol table, regardless of the presence of a local symbol table.
    • L: Macro variable is stored in the most local symbol table available, which may be the global symbol table.
    • F: It tells CALL SYMPUTX to use the version of the macro variable in the most local symbol table where it exists. If the macro variable does not exist, it will be stored in the most local symbol table.
Assign Number of Rows to Macro Variable

Here a data step is used to determine the number of observations in the sashelp.class dataset and assign that value to the macro variable nrows.

CALL SYMPUTX

data _null_;
if 0 then set sashelp.class nobs=n;
call symputx ('nrows',n);
run;
%put no. of rows = &nrows.;

LOG

no. of rows = 19

CALL SYMPUT

data _null_;
if 0 then set sashelp.class nobs=n;
call symput ('nrows',n);
run;
%put no. of rows = &nrows.;

LOG

  
NOTE: Numeric values have been converted to character values  
no. of rows =           19

You must have observed above the leading spaces in the macro variable nrows when using CALL SYMPUT. It also returns a note about the conversion of numeric values to character values.

Difference between CALL SYMPUT and CALL SYMPUTX

Below is a list of differences between CALL SYMPUT and CALL SYMPUTX.

  1. CALL SYMPUTX does not generate a note in the SAS log when the second argument is numeric. Whereas, CALL SYMPUT produces a log note stating the conversion of numeric values to character values.
  2. CALL SYMPUTX removes both leading and trailing blanks. Conversely, CALL SYMPUT does not remove leading blanks and only removes trailing blanks.
  3. When converting a numeric second argument to a character value, CALL SYMPUTX uses a field width of up to 32 characters, whereas CALL SYMPUT uses a field width of up to 12 characters.
  4. CALL SYMPUTX allows you to specify the symbol table in which to store the macro variable, whereas CALL SYMPUT does not.
Practical Example

Suppose you are asked to extract the name and age of the first student from "sashelp.class" dataset and stores them in macro variables.

data _null_;
set sashelp.class;
if _N_ = 1 then do;
call symputx('myname', name);
call symputx('myage', Age);
end;
run;

%put Name = &myname.;
%put Age = &myage.;
  • The if _N_ = 1 then do; statement tells SAS to run the code only for the first observation.
  • call symputx('myname', name); assigns the value of the variable "name" from the current observation to the macro variable "myname"
  • call symputx('myage', Age); assigns the value of the variable "Age" from the current observation to the macro variable "myage"
Related Posts
Spread the Word!
Share
About Author:
Deepanshu Bhalla

Deepanshu founded ListenData with a simple objective - Make analytics easy to understand and follow. He has over 10 years of experience in data science. During his tenure, he worked with global clients in various domains like Banking, Insurance, Private Equity, Telecom and HR.

Post Comment 0 Response to "SAS: CALL SYMPUT and CALL SYMPUTX"
Next → ← Prev