Subscribe to DSC Newsletter

CALL SYMPUT in SAS Explained with Examples

CALL SYMPUT and CALL SYMPUTX


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 are used to assign a value of DATA step to a macro variable.

SYNTAX:

CALL SYMPUT(macro-variable, value);


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

  • a character string that is a SAS name, enclosed in quotation marks.
  • It can contain the name of a character variable.
  • A character expression to create a macro variable name. You can create a series of macro variables using this form.

value is the value to be assigned, which can be

  • a string enclosed in quotation marks.
  • It can be the name of a numeric or character variable. The macro variable is assigned with the value of the variable,
  • a DATA step expression. The value returned by the expression in the current observation is assigned as the value of macro-variable.

Example 1: Using CALL SYMPUT to create macro variables of DATA step variables.


This DATA step creates the three macro variables countries, and respectively assign them the values in the code variable.

data currency_codes;
input country $ 6. code $4.;
call symput(country,code);
datalines;
USA USD
UK GBP
Japan JPY
Europe EUR
India INR
;
run;
%put &India;

OUTPUT

INR

Example 2: Creating a series of macro variable

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 $ 6. code $4.;
call symput('Country'||left(_n_),country);
datalines;
USA USD
UK GBP
Japan JPY
Europe EUR
India INR
;
run;
%put &country2;

OUTPUT

UK

Below is an example of a string enclosed in quotation marks  the statement assigns the string testing to the macro variable NEW:

data test;
call symput('new','testing');
run;
%put &new;

OUTPUT

testing

Example- When the value is the name of a numeric or character variable.

The macro variable is assigned with the value of the variable,


For numeric variable, SAS performs an automatic numeric-to-character conversion and writes a message in the log.

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

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.

For character variables, SYMPUT creates only one macro variable, and its value changes in each iteration of the program. 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);
run;
%put &name;

OUTPUT

William

Example when the value contains a  DATA step expression.

The value returned by the expression in the current observation is assigned as the value of macro-variable. In this example, the macro variable named HOLDATE receives the value July 4, 1997:

Also, Read the Date Interval Functions – INTNX and INTICK in SAS

Views: 217

Tags: programming, sas

Comment

You need to be a member of Data Science Central to add comments!

Join Data Science Central

Videos

  • Add Videos
  • View All

© 2020   TechTarget, Inc.   Powered by

Badges  |  Report an Issue  |  Privacy Policy  |  Terms of Service