DATA Step, Macro, Functions and more

Conditionally Modifying a Macro Variable within a data step

Reply
N/A
Posts: 0

Conditionally Modifying a Macro Variable within a data step

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
N/A
Posts: 0

Re: Conditionally Modifying a Macro Variable within a data step

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.
N/A
Posts: 0

Re: Conditionally Modifying a Macro Variable within a data step

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
SAS Super FREQ
Posts: 8,743

Re: Conditionally Modifying a Macro Variable within a data step

You might also investigate the use of SYMGET/SYMGETN. Remember that &MACVAR references are ALSO resolved at COMPILE time and not EXECUTION time.

cynthia
Super Contributor
Super Contributor
Posts: 3,174

Re: Conditionally Modifying a Macro Variable within a data step

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.
Ask a Question
Discussion stats
  • 4 replies
  • 126 views
  • 0 likes
  • 3 in conversation