CALL SYMPUT in SAS – Explained


CALL SYMPUT and CALL SYMPUTX in SAS are the most commonly used DATA Step call routines to interact with the Macro Facility in SAS. Both assign a value of the DATA step to a macro variable. ​In this article, we will demonstrate some of the uses and important facts of these two call Routines.



CALL SYMPUT(macro-variable, value);

Arguments are macro-variable and can be one of the following items:

macro-variable is a character string that is a SAS name, enclosed in quotation marks.

For example, to assign the character string ‘macrovar‘ to macro variable newMacro, submit the following statement in the data step:

call symput('newMacro','macrovar');

 It can contain the name of a character variable.

Using CALL SYMPUT to create macro variables of DATA step variables.

This DATA step creates the three macro variables countries and assigns the values in the code variable.

data currency_codes;
input country $ code $4.;
call symput('country', code);
Japan JPY
Europe EUR
India INR

This is an example of a character expression to create a macro variable name. You can create a series of macro variables using this form.

Creating a series of macro variables using CALL SYMPUT

In the below example, the CALL SYMPUT statement builds a series of macro variable names by combining the character string Country and the left-aligned value of _N_.

Values are assigned to the macro variables Country1, Country2, and so on.

data currency_codes;
	input country $ code $4.;
	call symput('Country'||left(_n_), country);
	Japan JPY
	Europe EUR
	India INR
%put &country2;

value is the value to be assigned, which can be a string enclosed in quotation marks.

For example, this statement assigns the string testing to the macro variable NEW:

data test;
	call symput('new', 'testing');

%put &new;

It can also be the name of a numeric or character variable. The macro variable is assigned with the value of the variable.

SAS performs an automatic numeric-to-character conversion for numeric variables and writes a message in the log.

data test;
	b="a character variable";
	call symput('a', a);
	call symput('b', b);

Use this form when macro-variable is also the name of a SAS variable or a character expression that contains a SAS variable.

A unique macro variable name and value can be created from each observation using the automatic data step variable _n_. See Example 2.

SYMPUT creates only one macro variable for character variables and its value changes in each program iteration. The macro variable contains only the value assigned in the last iteration of the data step.

data class;
	set sashelp.class;
	call symput('Name', name);
%put &name;

It can be a DATA step expression. The value returned by the expression in the current observation is assigned as the value of the macro-variable.

In this example, the macro variable named HOLDATE receives the value July 4, 1997:

data ex;
	input holiday mmddyy.;
	call symput('holdate', trim(left(put(holiday, worddate.))));

If a macro-variable does not exist, SYMPUT creates it. SYMPUT makes a macro variable assignment when the program executes.

Creating many macro variables using do loop and array

There are situations where you need to convert all of the dataset’s variables into a macro variable.

data _null_;
set sashelp.class;
suffix=put(_n_, 5.);
array num _NUMERIC_;
do i=1 to dim(num);
call symputx(cats(vname(num[i]), suffix), num[i]);
array char _CHARACTER_;
do i=1 to dim(char);
call symputx(cats(vname(char[i]), suffix), char[i]);

%put &name2;
%put &age2;

As in the above example, we appended the suffix created from the automatic variable _n_ onto the variable name supplied by the function VNAME. 

The SAS system requires separate array statements for characters and numeric.

Using the _NUMERIC_ and _CHARCATER_, we can assign all the dataset variables to these arrays

Then, we can use the “*” in the array definition and the DIM function in the loop to the number of variables.

The data step will convert all data set variables to macro variables with the same name and observation suffix.

However, many system resources may be used to store the macro variables if the dataset has hundreds of observations and variables.

Scope of Variables Created with SYMPUT

SYMPUT puts the macro variable in the most local nonempty symbol table. A symbol table is nonempty if it contains the following:

  • a value
  • computed %GOTO (A computed %GOTO contains % or & and resolves to a label.)
  • The macro variable &SYSPBUFF, created at macro invocation time.

However, there are three cases where SYMPUT creates the variable in the local symbol table, even if that symbol table is empty:

  • If SYMPUT is used after a PROC SQL, the variable will be created in a local symbol table.
  • If an executing macro contains a computed %GOTO statement and uses SYMPUT to create a macro variable, the variable is created in the local symbol table.
  • If an executing macro uses &SYSPBUFF and SYMPUT to create a macro variable, the macro variable is created in the local symbol table.

Problem Trying to Reference an SYMPUT-Assigned Value Before It Is Available

SYMPUT assigns the value of a macro variable during program execution, whereas macro variable references resolve during the compilation of a step.

As a result, You cannot use a macro variable reference to retrieve the value of a macro variable in the same program (or step) used to create and assign that macro variable.

You must specify a step boundary such as a RUN statement or another DATA or proc statement to force the DATA step to execute before referencing a value in a global statement.

data test;
	var='Macro Variable';
	call symput('mvar', var);

proc print;
	title "Value of &var";
CALL SYMPUT in SAS – Explained

Why there are trailing blanks when a macro variable is created using SYMPUT?

SYMPUT writes the value using $w. for the character variables. w is the length of the variable. Therefore, a value shorter than the length of the program variable is written with trailing blanks.

For example, in the following DATA step, the length of variables C1 and D1 is 8 by default. Therefore, SYMPUT uses $8. format and assigns the letter x followed by seven trailing blanks as the value of C1. For variable D1, the length of 8 is filled, so there are no trailing blanks.

To remove the blanks, use the TRIM function shown in the second SYMPUT statement.

data char1;
	input c $ d $;
	call symput('c1', c);
	call symput('c2', trim(c));
	call symput('d1', d);
x x1234567;

%put c1 = ***&c1***;
%put c2 = ***&c2***;
%put d1 = ***&d1***;

When this program executes, these lines are written to the SAS log:

c1 = ***x       ***c2 = ***x***d1 = ***x1234567***

For numeric variables, SYMPUT writes it using the BEST12. format. The resulting value is a 12-byte character with the value right-aligned within it.

For example, this DATA step assigns the value of the numeric variable n to the macro variables N1 and N2 and N3. The second CALL SYMPUT statement deletes leading blanks by using the LEFT function to left-align the value before the SYMPUT routine assigns the value to N2.

Because of this automatic variable conversion, SAS writes a note in the log. It is a best practice to explicitly convert the numeric variable to avoid the Note in the log.

The third SYMPUT statement converts the variable to a character and removes the leading blanks.

data _null_;
	call symput('n1', n);
	call symput('n2', left(n));
	call symput('n3', trim(left(put(n, 8.))));
%put n1 = ***&n1***;
%put n2 = ***&n2***;
%put n3 = ***&n3***;

When this program executes, these lines are written to the SAS log:

n1 = *** 10***n2 = ***10         ***n3 = ***10***

Also Read, Creating macro variables from SAS dataset


Assigns a value to a macro variable and removes both leading and trailing blanks.


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

The value of the symbol table can be any of the following.

  • G – It specifies that the macro variable is stored in the global symbol table, even if the local symbol table exists.
  • L – specifies that the macro variable is stored in the most local symbol table, which will be the global symbol table if used outside a macro.
  • F specifies that if the macro variable exists in any symbol table, CALL SYMPUTX uses the version in the most local symbol table in which it exists. If the macro variable does not exist, CALL SYMPUTX stores the variable in the most local symbol table.

Main Benefits of using CALL SYMPUTX over CALL SYMPUT:

  • No Notes in SAS LogCALL SYMPUTX does not write a note to the SAS log when the second argument is numeric.
  • More widthCALL SYMPUTX uses a field width of up to 32 characters when it converts a numeric second argument to a character value. CALL SYMPUT uses a field width of up to 12 characters.
  • No Leading BlanksCALL SYMPUTX left-justifies both arguments and trims trailing blanks. CALL SYMPUT does not left-justify the arguments and only trims trailing blanks from the first argument. Leading blanks in the value of the name cause an error.
  • Control on Macro symbol table CALL SYMPUTX enables you to specify the symbol table in which to store the macro variable, whereas CALL SYMPUT does not.

Some FAQ’s

Where can you use the macro variable if you use a CALL SYMPUT in a DATA step?

Macro variables created using the CALL SYMPUT routine cannot be used in the same data step in which it got created. Other than that, we can use the macro variable anywhere.

What is the maximum length of the macro variable?

Macro variables are 32 characters long.

What is the difference between SYMGET and SYMPUT?

SYMPUT puts the value from a dataset into a macro variable, whereas SYMGET gets the value from the macro variable to the dataset.

What is the difference between %LOCAL and %GLOBAL?

% Local is a macro variable defined inside a macro.%Global is a macro variable defined in open code (outside the macro or can use anywhere)

Every week we'll send you SAS tips and in-depth tutorials



Subhro provides valuable and informative content on SAS, offering a comprehensive understanding of SAS concepts. We have been creating SAS tutorials since 2019, and 9to5sas has become one of the leading free SAS resources available on the internet.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.