Hi, I have a variable X with the sequence of number such as: 3, 4, 6, 5, 4, 7, 9, 7, 8, 9, 10. I need to create a new variable Y so that Yi=max(Xi, Yi-1). It would become a sequence like: 3, 4, 6, 6, 6, 7, 9, 9, 9, 9, 10. How can I do the programming to get this new variable? Thank you so much for your suggestion.
Hi @hansmuller
I am thinking about something like this, using the RETAIN function:
data want;
set have;
retain y;
if x >= y then y=x;
run;
Hi @hansmuller,
Apply the RETAIN statement to y:
data have;
infile cards dsd;
input x @@;
cards;
3, 4, 6, 5, 4, 7, 9, 7, 8, 9, 10
;
data want;
set have;
y=max(x,y);
retain y;
run;
Hi @hansmuller
I am thinking about something like this, using the RETAIN function:
data want;
set have;
retain y;
if x >= y then y=x;
run;
Alternatively with ifn and retain
data want;
set have;
retain new;
new=ifn(x<new,new,x);
run;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.
Ready to level-up your skills? Choose your own adventure.