Keeping the first record for each date, per ID. But keeping them for separate locations.

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 6
Accepted Solution

Keeping the first record for each date, per ID. But keeping them for separate locations.

[ Edited ]

I've always gotten the help I needed here, so I'm asking for expertise once again.

 

I have data like this (sorry it's not that creative): 

IDServiceDateLocation
Acleaning5/22/20181
Awashing5/22/20181
Acooking5/23/20182
Borganizing5/23/20183
Bwashing5/23/20183
Bcooking5/23/20183
Ccooking5/21/20184
Corganizing5/22/20181
Dcleaning5/19/20181
Dwashing5/20/20181
Dcooking5/21/20184
Dorganizing5/21/20185

 

It includes other variables too, but ultimately, I want to be able to produce at least something like this:

IDServiceDateLocation
Acleaning5/22/20181
Acooking5/23/20182
Borganizing5/23/20183
Ccooking5/21/20184
Corganizing5/22/20181
Dcleaning5/19/20181
Dwashing5/20/20181
Dcooking5/21/20184
Dorganizing5/21/20185

It kept only one "service" (doesn't matter which one) per that day (Date), for that person (ID).  I would normally use "if first.id;" in a data step, but I would'nt know how to do that involving per date and only for same locations. Notice ID "D" kept both services ("cooking" and "organizing") for the same date ("5/21/2018") because it happened at two different locations ("4" and "5").

 

If would be best though to have something like this, tabulated, if possible:

IDServiceDateLocationSevice_2Location_2Service_3Location_3
Acleaning5/22/20181washing1  
Acooking5/23/20182    
Borganizing5/23/20183washing3cooking3
Ccooking5/21/20184    
Corganizing5/22/20181    
Dcleaning5/19/20181    
Dwashing5/20/20181    
Dcooking5/21/20184    
Dorganizing5/21/20185    

 

Thank you in advance for your input.


Accepted Solutions
Solution
‎05-29-2018 02:25 PM
Super User
Posts: 13,563

Re: Keeping the first record for each date, per ID. But keeping them for separate locations.

You were likely close but you didn't show your code so hard to tell:

Try something like this:

proc sort data=have;
   by id location date;
run;

data want;
  set have;
  by id location date;
  if first.location;
run;

 

For almost anything except a proc print report you do not want to use that wide format. You have to keep track of a bunch of variables that are not populated for each resulting record, if you do this process with a new data set you may have a differing number of "wide" columns that means your code developed from the point of making the "wide" data set may not run because of referencing variables no longer there OR failing because it does not address added columns.

 

What will the next steps of your process be?

View solution in original post


All Replies
Solution
‎05-29-2018 02:25 PM
Super User
Posts: 13,563

Re: Keeping the first record for each date, per ID. But keeping them for separate locations.

You were likely close but you didn't show your code so hard to tell:

Try something like this:

proc sort data=have;
   by id location date;
run;

data want;
  set have;
  by id location date;
  if first.location;
run;

 

For almost anything except a proc print report you do not want to use that wide format. You have to keep track of a bunch of variables that are not populated for each resulting record, if you do this process with a new data set you may have a differing number of "wide" columns that means your code developed from the point of making the "wide" data set may not run because of referencing variables no longer there OR failing because it does not address added columns.

 

What will the next steps of your process be?

Super User
Posts: 6,781

Re: Keeping the first record for each date, per ID. But keeping them for separate locations.

Notice that your preferred result only requires one Location field.  Here's a way to get that:

 

proc transpose data=have prefix=Service_ out=want (drop=_name_);

by id date location;

var service;

run;

☑ This topic is solved.

Need further help from the community? Please ask a new question.

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