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
SAS Super FREQ
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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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