• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / SAS / SAS call symput – Create Macro Variables in Data Step

SAS call symput – Create Macro Variables in Data Step

September 23, 2022 Leave a Comment

In SAS, the call symput function allows you to create macro variables in a SAS data step. You can assign both numeric and character values to macro variables with call symput.

data example;
    call symput('var1', "example value");
    call symput('var2', 2);
run;

%put &var1;

/* Log Output: */
example value
2

When working with SAS, the SAS Macro Language allows you to create dynamic and powerful programs. At the core of the SAS Macro Language are macro variables and the ability to easily create macro variables is important.

One such way you can create macro variables, or assign values to macro variables, in SAS is with call symput in a data step.

You can assign both numeric and character values to macro variables with call symput.

Below is a simple example showing you how you can use call symput to assign a value to a macro variable in SAS.

data example;
    call symput('var1', "example value");
    call symput('var2', 2);
run;

%put &var1;

/* Log Output: */
example value
2

Format of Macro Variables After Using call symput in SAS

There are a few things that you should know about what happens when you use call symput in a data step.

First, for character values, call symput by default writes values with the same length as the original data step variable. If the length of the variable in the SAS data step is 20 and you have a value like “bobby”, then there will be 15 trailing blanks in the macro variable.

For numeric values, symput writes these values using the best12. format. The resulting output to the log is a 12-byte character representing the numeric value and is right aligned.

Using call symput in Loop in Data Step

You can use call symput in a loop if you have similarly named variables or are looping over a list of variables defined in a macro variable.

Then, in the loop, you can assign values to multiple macro variables.

Below shows an example of how you could use call symput in a loop in a SAS data step.

data example;
    input var1 var2 var3;
    datalines;
    3 4 5
    ;
run;

data want;
   set example;
   array v[3] var1-var3;
   do i = 1 to 3;
       call symput(cats('var',i),v[i]);
   end;
run;

%put &var1;
%put &var2;
%put &var3;

/* Log Output */
3
4
5

You can also create macro variables in a macro as shown below. Note, these macro variables will not be available for use outside of a macro if you do it in this way because call symput creates the macro variables in the most local macro variable table.

data example;
    input var1 var2 var3;
    datalines;
    3 4 5
    ;
run;

%macro loop;

data want;
   set example;
   %do i = 1 %to 3;
       call symput(cats('var',&i), var&i);
   %end;
run;

%put &var1;
%put &var2;
%put &var3;

%mend;

%loop;

/* Log Output */
3
4
5

Hopefully this article has been useful for you to learn how to use call symput in SAS.

Other Articles You'll Also Like:

  • 1.  SAS Delete Dataset – Use PROC Datasets to Delete SAS Files
  • 2.  Set Multiple Datasets in SAS Data Step
  • 3.  How to Select First 100 Observations of SAS Dataset
  • 4.  Multiple Condition If Statements in SAS Macro Language
  • 5.  SAS yymmdd10. Date Format
  • 6.  SAS Dollar Format – Formatting Numbers as Dollars in SAS Dataset
  • 7.  SAS left() Function – Left Align Character Variables in Data Step
  • 8.  nodup vs nodupkey PROC SORT Options in SAS
  • 9.  SAS ceil – Round Up to Ceiling of Number in a SAS Data Step
  • 10.  SAS calculated – Use Columns Created from Select in PROC SQL

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy