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
SAS Super FREQ

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