Hi!
I'm trying to create a counter variable with multiple conditions. Please see below: the table includes the data in days and the counter I'm trying to program.
1. This is all within a subject. If first subject then start counter = 1.
2. if Days >120 and the lag of the previous Day > 60 then counter =1 if this is the first observation meeting this criteria,
3. If Days >120 and lag of previous Day is <60 then don't add to the counter, rather this observation gets the same as the previous counter number.
4. If Days greater than 0 and LE 120 then +1 to the counter and all other observations <120 are the same counter value.
5. If Days is <0 then counter+1 for each instance.
Thank you for any and all assistance.
Days | Counter |
2514 | 1 |
141 | 2 |
99 | 3 |
54 | 3 |
0 | 3 |
-205 | 4 |
-290 | 5 |
Your rules are incomplete. If days > 120 and previous day = 60 exactly, you don't say what should happen. If days is exactly equal to zero, you don't say what should happen.
Also, your rules 1 and 2 seem to indicate that the counter starts at 1, then it can change, and then go back to 1 if rule 2 is satisfied, is this correct?
Hi Paige,
Thanks for your reply. The 60 should be <= 60 and >= 0. Yes, the counter starts at 1 each time, but will advance sequentially and not reset at 1 at any point.
I so appreciate your help and responsiveness.
data want;
set have;
by id;
if first.id then counter=1;
prev_days=lag(days);
if days>120 and prev_day>60 then counter=1; /* This condition still doesn't make sense to me */
else if days>=0 and days<=120 and prev_day>120 then counter+1;
else if days<0 then counter+1;
run;
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: