How do I change this conditional statement in to Macro? ( when event_name 1 then pmdWk=24, when event_name 2= then pmdWk=24+24=48, ..........)
data Smerge;
merge test
fmerge;
by Id ;
length pmdWk 8.;
pmdWk = scan( PmdVisit, 2, '_');
length event_name 8.;
event_name=scan(RD_event_name, 2,'_');
if event_name= '1' then Npmdwk= pmdWk;
if event_name= '2' then Npmdwk= sum(pmdWk+24);
if event_name= '3' then Npmdwk= sum(pmdWk+48);
if event_name= '4' then Npmdwk= sum(pmdWk+72); and continues...........
Change what?
How?
What are you trying to do?
Where does macro code come in? Macros write code. So you would start with a complete working data step first.
And you show no macro code so haven't got a starting place. Note that the type of DATA manipulation is typically not the place for macros.
I might guess if the goal is to add multiples of 24 to a variable:
data Smerge; merge test fmerge; by Id ; length pmdWk 8.; pmdWk = scan( PmdVisit, 2, '_'); length event_name 8.; event_num=input(scan(RD_event_name, 2,'_'),best.); Npmdwk= sum(pmdWk, (event_num-1)*24 ); run;
Sometimes algebra is your friend.
This might have popped out easier if you think of
if event_name= '1' then Npmdwk= pmdWk;
as
if event_name= '1' then Npmdwk= sum( pmdWk, 0);
Npmdwk= sum(pmdWk,(input(event_name,best.) - 1) * 24);
Since this is a data issue, no macro is needed.
@hjjijkkl wrote:
Thank you KurtBremser!
But, How do I incorporate your code with the if/then statement ?
Thank you!
The code provided does exactly what you want ... try it.
@hjjijkkl wrote:
Thank you KurtBremser!
But, How do I incorporate your code with the if/then statement ?
Thank you!
No need for if/then. The code solves this with the formula. Try it (Maxim 4).
Change what?
How?
What are you trying to do?
Where does macro code come in? Macros write code. So you would start with a complete working data step first.
And you show no macro code so haven't got a starting place. Note that the type of DATA manipulation is typically not the place for macros.
I might guess if the goal is to add multiples of 24 to a variable:
data Smerge; merge test fmerge; by Id ; length pmdWk 8.; pmdWk = scan( PmdVisit, 2, '_'); length event_name 8.; event_num=input(scan(RD_event_name, 2,'_'),best.); Npmdwk= sum(pmdWk, (event_num-1)*24 ); run;
Sometimes algebra is your friend.
This might have popped out easier if you think of
if event_name= '1' then Npmdwk= pmdWk;
as
if event_name= '1' then Npmdwk= sum( pmdWk, 0);
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.