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

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 634 views
  • 0 likes
  • 3 in conversation