Hello. I am trying to create an enumeration variable that must take into account 3 by groups. I need to get two different variables - one restarts by patient ID and the other keeps counting. All the solutions online and on this forum are not working. Please assist. This is what I am going for:
PID RID LID RestartVariable CountVariable (trying to get the last two variables)
141 57 19 1 1
141 57 19 1 1
141 58 13 2 2
141 58 14 3 3
141 59 20 4 4
141 59 20 4 4
142 59 25 1 5
142 59 25 1 5
142 59 25 1 5
142 60 26 2 6
143 60 26 1 7
Thank you.
Like this?
EDIT: I edited my code because you changes your question. I think this is what you want. But shouldn't RestartVariable=1 at the bottom?
EDIT2: I see you changed your question again 🙂 @eabc0351 does this work for you?
data have;
input PID RID LID;
datalines;
141 57 19
141 57 19
141 58 13
141 58 14
141 59 20
141 59 20
142 59 25
142 59 25
142 59 25
142 60 26
143 60 26
;
data want;
set have;
by PID notsorted RID notsorted LID notsorted;
if first.PID | first.RID | first.LID then do;
CountVariable+1;
RestartVariable+1;
end;
if first.PID then RestartVariable=1;
run;
Like this?
EDIT: I edited my code because you changes your question. I think this is what you want. But shouldn't RestartVariable=1 at the bottom?
EDIT2: I see you changed your question again 🙂 @eabc0351 does this work for you?
data have;
input PID RID LID;
datalines;
141 57 19
141 57 19
141 58 13
141 58 14
141 59 20
141 59 20
142 59 25
142 59 25
142 59 25
142 60 26
143 60 26
;
data want;
set have;
by PID notsorted RID notsorted LID notsorted;
if first.PID | first.RID | first.LID then do;
CountVariable+1;
RestartVariable+1;
end;
if first.PID then RestartVariable=1;
run;
Hi @PeterClemmensen. Yes, I messed up the bottom number. This solution works for both variables. Thank you for your assistance.
Anytime, glad to help 🙂
Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.
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.