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

Yes, I did not mean to mark that as accepted. Mouse malfunction...

WAL83
Obsidian | Level 7

Your code IS resetting pair=1 but it deleted a record for some reason. Original dataset has 49 records, output dataset now has 48. Your code deletes the record in red... It was the last record for that ID and did not have a PAIR...

 

ID Start_Date Start_Time End_Time Value
2003 1-Jul-13 6:00:00 21:59:00 DAY
2003 1-Jul-13 22:00:00 5:59:00 NIGHT
2003 2-Jul-13 22:00:00 5:59:00 NIGHT
2003 3-Jul-13 6:00:00 21:59:00 DAY
2003 3-Jul-13 22:00:00 5:59:00 NIGHT
2003 4-Jul-13 6:00:00 21:59:00 DAY
2003 4-Jul-13 22:00:00 5:59:00 NIGHT
2003 5-Jul-13 6:00:00 21:59:00 DAY
art297
Opal | Level 21

Indeed! I missed the case where it was the last.id and started with an otherwise potentially valid record. This code should correct for that:

 

data want (drop=_:);
  set have;
  by id;
  length action $6;
  retain _next_keep _last_kept_date _pair;
  if first.id then _pair=0;
  if not last.id then do;
    _next=_n_+1;
    set have (keep=start_date value
         rename=(start_date=_next_start
                 value=_next_value)) point=_next;
    if _next_keep='KEEP' then do;
      action='KEEP';
      _last_kept_date=start_date;
      pair=_pair;
      output;
      _next_keep='DELETE';
    end;
    else if start_date eq _next_start and
            value eq 'DAY' and
            _next_value eq 'NIGHT' then do;
      if 0<=(start_date-_last_kept_date)<=1 then _pair+1;
      else _pair=1;
      pair=_pair;
      action='KEEP';
      output;
      _next_keep='KEEP';
    end;
    else do;
      action='DELETE';
      output;
      _next_keep='DELETE';
    end;
  end;
  else do;
    if _next_keep eq 'KEEP' then do;
      action='KEEP';
      pair=_pair;
      output;
      _next_keep='DELETE';
    end;
    else do;
      action='DELETE';
      output;
      _next_keep='DELETE';
    end;
  end;
run;

Art, CEO, AnalystFinder.com

 

WAL83
Obsidian | Level 7

Thanks for hanging in there!!! I realize what I was asking was not exactly clear at first. Will be more concise the next time. Thanks again!!!!

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 18 replies
  • 1687 views
  • 5 likes
  • 4 in conversation