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

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.

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

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;

 

 

View solution in original post

3 REPLIES 3
PeterClemmensen
Tourmaline | Level 20

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;

 

 

eabc0351
Quartz | Level 8

Hi @PeterClemmensen. Yes, I messed up the bottom number. This solution works for both variables. Thank you for your assistance.

PeterClemmensen
Tourmaline | Level 20

Anytime, glad to help 🙂

sas-innovate-white.png

Special offer for SAS Communities members

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.

 

View the full agenda.

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
  • 3 replies
  • 1804 views
  • 0 likes
  • 2 in conversation