BookmarkSubscribeRSS Feed
Kimbraun1
Calcite | Level 5

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.

 

 

DaysCounter
25141
1412
993
543
03
-2054
-2905
3 REPLIES 3
PaigeMiller
Diamond | Level 26

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?

--
Paige Miller
Kimbraun1
Calcite | Level 5

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.

PaigeMiller
Diamond | Level 26
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;
--
Paige Miller

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 891 views
  • 0 likes
  • 2 in conversation