BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
jins
Fluorite | Level 6

Trying to get this manipulation going and any thoughts/help on this is appreciated.

Dataset Sample

id    status   date

1     OK           02-MAY-09

1    OK          10-MAY-09

1    DEL        01-JUL-22

1    OK          01-DEC-22

1    OK          04-DEC-22

1    DEL        06-DEC-22

2   DEL        01-JAN-23

3   OK          03-JAN-23

4   DEL        04-JAN-23

4  DEL         05-JAN-23

4  OK         06-JAN23

 

Looking for the following output which can be long or wide format

1     OK          02-MAY-09

1    DEL        01-JUL-22

1    OK          01-DEC-22

1    DEL        06-DEC-22

2   DEL        01-JAN-23

3   OK          03-JAN-23

4   DEL        04-JAN-23

4  OK          06-JAN23

 

Thanks in advance.

 

 

     

1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

Same selection logic as already suggested by @jins 

data have;
  infile datalines truncover;
  input id status $ dt:date11.;
  format dt date9.;
  datalines;
1 OK 02-MAY-09
1 OK 10-MAY-09
1 DEL 01-JUL-22
1 OK 01-DEC-22
1 OK 04-DEC-22
1 DEL 06-DEC-22
2 DEL 01-JAN-23
3 OK 03-JAN-23
4 DEL 04-JAN-23
4 DEL 05-JAN-23
4 OK 06-JAN23
;

/* ensure source data is sorted as required */
proc sort data=have presorted;
  by id dt;
run;

data want;
  set have;
  by id status notsorted;
  if first.status;
run;

proc print data=want;
run;

 

View solution in original post

5 REPLIES 5
SASJedi
Ammonite | Level 13

Can you explain the logic for choosing the rows to output?

 

Check out my Jedi SAS Tricks for SAS Users
jins
Fluorite | Level 6

I should have mentioned that oops....

To obtain the date of the first occurrence between the status change by ID. For example ID-1 has two OK status before the DEL status, so we keep the first one with date 02-May-2009. For ID's with no status changes, it is output as is (ID-2).

 

jins
Fluorite | Level 6

Is the notsorted option in the by variable a good idea to filter them out if the data is already sorted prior by id, date and status?

data check;

by id status notsorted;

if first.status=1;

run;

 

 

 

Patrick
Opal | Level 21

Same selection logic as already suggested by @jins 

data have;
  infile datalines truncover;
  input id status $ dt:date11.;
  format dt date9.;
  datalines;
1 OK 02-MAY-09
1 OK 10-MAY-09
1 DEL 01-JUL-22
1 OK 01-DEC-22
1 OK 04-DEC-22
1 DEL 06-DEC-22
2 DEL 01-JAN-23
3 OK 03-JAN-23
4 DEL 04-JAN-23
4 DEL 05-JAN-23
4 OK 06-JAN23
;

/* ensure source data is sorted as required */
proc sort data=have presorted;
  by id dt;
run;

data want;
  set have;
  by id status notsorted;
  if first.status;
run;

proc print data=want;
run;

 

jins
Fluorite | Level 6

thanks.

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