• 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 let – Create User-Defined Macro Variables in Your SAS Code

SAS let – Create User-Defined Macro Variables in Your SAS Code

January 25, 2022 Leave a Comment

We use the SAS let statement to define macro variables to use in our code. The variables created are character strings or text expressions.

%let var1 = This is a macro variable defined by the SAS let statement;

The SAS Macro Language is very powerful and allows us to create dynamic SAS programs. The basis of the Macro Language is the use of macro variables.

To create macro variables, we can use the SAS let statement.

The let statement allows us to create macro variables which are character strings or text expressions. The user-defined macro variable then goes to the table of macro variables. let removes trailing and leading blanks from the input expression.

%let var1 = This is a macro variable defined by the SAS let statement;

To use the macro variable later in your code, you can access it with “&” in front of the variable name.

%let var1 = This is a macro variable defined by the SAS let statement;

%put &var1;

/* Output */
This is a macro variable defined by the SAS let statement

Using the SAS let statement to Create a List of Strings

One useful way we can use the let statement in SAS is to create a list of strings. We can create a list of strings and then use the SAS scan() function in a loop to loop through each string in our macro variable.

In the SAS code below, we define a list of strings in the macro variable “list” and then loop over it with the help of the SAS countw() function.

%let list = NY TX FL MN OH TN MI OK;

%macro scan_example;
%do i = 1 to %sysfunc(countw(&list));
    %let current_state = %scan(&list,&i);
    %if &current_state = TX %then %do;
        /* Do Texas stuff here */
    %end;
%end;
%mend;

Hopefully this article has been useful for helping you learn how to define macro variables in your SAS code with a let statement.

Other Articles You'll Also Like:

  • 1.  Get Substring from Right in SAS
  • 2.  nodup vs nodupkey PROC SORT Options in SAS
  • 3.  SAS Delete Dataset – Use PROC Datasets to Delete SAS Files
  • 4.  Round Number to Nearest Integer in SAS
  • 5.  SAS min() Function – Find Minimum Value Across Columns in Data Step
  • 6.  SAS yymmdd10. Date Format
  • 7.  SAS prxmatch – Find Pattern in String Using Regex (Regular Expression)
  • 8.  Multiple Condition If Statements in SAS Macro Language
  • 9.  SAS Lowercase – Make String Lowercase with SAS lowcase function
  • 10.  SAS data _null_ – Create a SAS Dataset with No Records and Variables

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