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

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.

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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

2 REPLIES 2
ballardw
Super User

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?

Astounding
PROC Star

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;

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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