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.
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;
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;
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).
It's your turn to help shape SAS Innovate 2027. Share your expertise and inspire the SAS community.
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.