Have: Data ordered by unique Code, by unique ID, with static Date1 and static Item1.
Code1 | Date1 | Item1 | Id |
1 | 2/23/21 | 5 | 1 |
1 | 2/23/21 | 9 | 2 |
1 | 2/23/21 | 6 | 3 |
2 | 2/23/21 | 7 | 1 |
2 | 2/23/21 | 3 | 2 |
3 | 2/23/21 | 4 | 1 |
Want: When ID eq 1 then Want1= Item1 + Date1, else add Item1 to Want1 while retaining the value of the previous Want1 until the end of the ID of Code1.
Code1 | Date1 | Item1 | Id | Want1 |
1 | 2/23/21 | 5 | 1 | 2/28/21 |
1 | 2/23/21 | 9 | 2 | 3/9/21 |
1 | 2/23/21 | 6 | 3 | 3/15/21 |
2 | 2/23/21 | 7 | 1 | 3/2/21 |
2 | 2/23/21 | 3 | 2 | 3/5/21 |
3 | 2/23/21 | 4 | 1 | 2/27/21 |
Code1 | Date1 | Item1 | Id | Want1 |
1 | 2/23/21 | 5 | 1 | 2/28/21 |
1 | 2/23/21 | 9 | 2 | 3/9/21 |
1 | 2/23/21 | 6 | 3 | 3/15/21 |
2 | 2/23/21 | 7 | 1 | 3/2/21 |
2 | 2/23/21 | 3 | 2 | 3/5/21 |
3 | 2/23/21 | 4 | 1 | 2/27/21 |
I have experimented with the code below but I'm unsure how to keep the created value of want1 when id =1, and then add item1 when id ne 1 by code1. Any thoughts or help is greatly appreciated.
data want; set have;
by code1 date1 id;
if first.id then want1 = item1+date1;
else ?
run;
I figured it out by utilizing retain.
data want; set have;
by code1 date1 id;
retain want1;
if id=1 then want1=date1+item1;
else want1= want1+ item1;
run;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.