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-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

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