• 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 %eval() Function – Evaluate Expressions in SAS Macro

SAS %eval() Function – Evaluate Expressions in SAS Macro

April 26, 2022 Leave a Comment

When working in the SAS Macro Language, you can use the %eval() function to evaluate logical expressions and perform integer arithmetic.

%let a = 3+5;
%let b = 10/5;
%let c = 10>3;
%let eval_a = %eval(&a);
%let eval_b = %eval(&b);
%let eval_c = %eval(&c);

%put &eval_a;
%put &eval_b;
%put &eval_c;

/* Output */
8
2
1

The SAS Macro Language allows us to create dynamic and powerful code which allows us to handle complicated code requirements.

One useful function from the SAS Macro Language is %eval().

When working in the SAS Macro Language, you can use the %eval() function to evaluate logical expressions and perform integer arithmetic.

Macro variables are strings, and so if you have a string which represents an expression, %eval() can help you get the value of that evaluated expression.

Below are some examples of how you can use the %eval() function in SAS to evaluate different expressions.

%let a = 3+5;
%let b = 10/5;
%let c = 10>3;
%let eval_a = %eval(&a);
%let eval_b = %eval(&b);
%let eval_c = %eval(&c);

%put &eval_a;
%put &eval_b;
%put &eval_c;

/* Output */
8
2
1

Using %eval() to Increment a Counter in a SAS Macro

One useful example of using the SAS Macro %eval() function is if you are using a loop and want to increment a counter.

If you are using a do loop in a SAS Macro, or just need to increment a SAS macro variable, you can use %eval().

Below is an example of a do loop in a SAS Macro where we use %eval() to increment a counter variable.

%let counter = 0;

%macro loop_example();
    %do i = 0 %to 3;
        %put &counter;
        %let counter = %eval(&counter+1);
    %end;
%mend;

%loop_example();

/* Output */
0
1
2
3

Hopefully this article has been useful for you to learn how to use the SAS eval() function in the SAS Macro Language.

Other Articles You'll Also Like:

  • 1.  SAS let – Create User-Defined Macro Variables in Your SAS Code
  • 2.  countw SAS – Count Number of Words in a String
  • 3.  Get Last Observation of SAS Dataset with end=
  • 4.  SAS Dollar Format – Formatting Numbers as Dollars in SAS Dataset
  • 5.  SAS sum() Function – Find Sum Across Columns in Data Step
  • 6.  SAS contains() – Check if Variable Contains a String in Where Statement
  • 7.  SAS Less Than or Equal to with LE or <=
  • 8.  SAS mean() Function – Find Average Across Columns in Data Step
  • 9.  SAS calculated – Use Columns Created from Select in PROC SQL
  • 10.  SAS weekday function – Get Day of Week from Date Variable

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