BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
hansmuller
Fluorite | Level 6

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. 

1 ACCEPTED SOLUTION

Accepted Solutions
ed_sas_member
Meteorite | Level 14

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;

View solution in original post

4 REPLIES 4
FreelanceReinh
Jade | Level 19

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;
ed_sas_member
Meteorite | Level 14

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;
Jagadishkatam
Amethyst | Level 16

Alternatively with ifn and retain

 

data want;
set have;
retain new;
new=ifn(x<new,new,x);
run;
Thanks,
Jag

sas-innovate-white.png

Missed SAS Innovate in Orlando?

Catch the best of SAS Innovate 2025 — anytime, anywhere. Stream powerful keynotes, real-world demos, and game-changing insights from the world’s leading data and AI minds.

 

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1214 views
  • 3 likes
  • 4 in conversation