BookmarkSubscribeRSS Feed
deleted_user
Not applicable
Hi all,
I was wondering is it possible to conditionally modify an existing Macro Variable within a data step. I am trying to do something like this within the data step:

If var> &number then do;
%Let Existing_Variable=%EVAL(&Existing_Variable+1);
End;

When I run that it appears to be modifying the macro variable &Existing_Variable even if the condition is not met. Does anyone know how to do something like this?

Thanks!

~Matt
4 REPLIES 4
deleted_user
Not applicable
Hi Matt,

It's the age old story. The difference between compilation time and execution time. Your %let will create the macro variable at compilation time (i.e. before any data has been run through, before the datastep has been executed). To update the value of a macro variable at execution time you need to use call symput.

I would recommend looking up some documentation so that you understand it but basically what you need is:

If var> &number then do;
call symput('Existing_Variable', &Existing_Variable+1);
End;

I'm not sure it will like referencing itself. Why not create the count variable as a sas variable then use a call symput at the end to put that count value into your macro variable.
deleted_user
Not applicable
pznew,

Thanks for the input. I had thought about using the call symput but was hoping to get around using it. I will go that route. Thanks for the suggestion and help.


~Matt
Cynthia_sas
Diamond | Level 26
You might also investigate the use of SYMGET/SYMGETN. Remember that &MACVAR references are ALSO resolved at COMPILE time and not EXECUTION time.

cynthia
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
For your diagnostic, debugging experience, consider adding the following SAS code to your program to reveal code as it is compiled and executed:

OPTIONS SOURCE SOURCE2 MACROGEN SYMBOLGEN MLOGIC;


Scott Barry
SBBWorks, Inc.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1236 views
  • 0 likes
  • 3 in conversation