BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
ursula
Pyrite | Level 9

Hi there,

if anyone can help would be greatly appreciated.

Thank you very much in advance.

 

I want to have an output as following - New VISDATE

data have:                                                    

ID visit VISDATE PAY New VISDATE 
A 10 2/18/2022 1 2/18/2022
A 11 4/18/2022 1 2/18/2022
A 12 5/6/2023 1 2/18/2022
A 13 10/6/2023 2 10/6/2023
A 14 7/8/2024 2 7/8/2024
A 15 12/8/2024 1 12/8/2024
A 16 2/28/2025 2 2/28/2025
A 17 9/25/2025 1 9/25/2025
A 18 12/7/2025 1 9/25/2025

 

so if PAY =1 then the new VISDATE = VISDATE, if the next visit has PAY =1 then carry forward the new VISDATE until PAY=2.

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
data have;
infile cards expandtabs;
input ID $	visit 	VISDATE	 :$20. PAY;
cards; 
A	10	2/18/2022	1
A	11	4/18/2022	1
A	12	5/6/2023	1
A	13	10/6/2023	2
A	14	7/8/2024	2
A	15	12/8/2024	1
A	16	2/28/2025	2
A	17	9/25/2025	1
A	18	12/7/2025	1
;

data want;
 set have;
 by id pay notsorted;
 retain new_visitdate;
 if first.pay and pay=1 then new_visitdate=VISDATE;
 if pay ne 1 then new_visitdate=VISDATE;
run;

View solution in original post

4 REPLIES 4
Ksharp
Super User
data have;
infile cards expandtabs;
input ID $	visit 	VISDATE	 :$20. PAY;
cards; 
A	10	2/18/2022	1
A	11	4/18/2022	1
A	12	5/6/2023	1
A	13	10/6/2023	2
A	14	7/8/2024	2
A	15	12/8/2024	1
A	16	2/28/2025	2
A	17	9/25/2025	1
A	18	12/7/2025	1
;

data want;
 set have;
 by id pay notsorted;
 retain new_visitdate;
 if first.pay and pay=1 then new_visitdate=VISDATE;
 if pay ne 1 then new_visitdate=VISDATE;
run;
ursula
Pyrite | Level 9
Thank you so much for the codes! it really helps! I love SAS community support!
PaigeMiller
Diamond | Level 26

Just a quick addition to the correct answer from @Ksharp : the RETAIN statement, when used properly, allows you to carry data forward to the next row, and allows it to change when your rules require it to change (when PAY=2 do not carry the data forward).

--
Paige Miller
ursula
Pyrite | Level 9
Thank you so much for the information. I really appreciate it.

Catch up on SAS Innovate 2026

Nearly 200 sessions are now available on demand with the SAS Innovate Digital Pass.

Explore 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
  • 432 views
  • 2 likes
  • 3 in conversation