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

Hi, 

 

I have a dataset:

 

 

ClientBusiness StageDate and Time
AAA120-Dec-18
AAA221-Dec-18
AAA324-Dec-18
AAA105-Jan-19
AAA306-Jan-19
BBB120-Dec-18
BBB221-Dec-18

 

I want to know how much time each client spent in each of the business stages.  I want:

ClientBusiness StageTime Spent (Days)
AAA12
AAA23
AAA312
BBB`11

 

The tricky thing is that each client can go back and fourth between stages. As you see here, Client AAA went to stage 1 twice. So client AAA spend 2 days in stage 1. 

 

Thanks for helping. 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

UNTESTED CODE (and assumes everything is sorted by client and date, and the dates are actual SAS date values)

 

data re_arrange;
    merge have have(firstobs=2 rename=(client=client2 date=date2) drop=stage);
    elapsed=date2-date1;
    if client2^=client then delete;
run;
proc summary data=re_arrange;
    class client stage;
    var elapsed;
    output out=want sum=sum_elapsed;
run;
--
Paige Miller

View solution in original post

3 REPLIES 3
PaigeMiller
Diamond | Level 26

UNTESTED CODE (and assumes everything is sorted by client and date, and the dates are actual SAS date values)

 

data re_arrange;
    merge have have(firstobs=2 rename=(client=client2 date=date2) drop=stage);
    elapsed=date2-date1;
    if client2^=client then delete;
run;
proc summary data=re_arrange;
    class client stage;
    var elapsed;
    output out=want sum=sum_elapsed;
run;
--
Paige Miller
kz134
Obsidian | Level 7
this worked. thank you
PeterClemmensen
Tourmaline | Level 20

A hash object approach

 

data have;
input Client $ Business_Stage Date:date9.;
format Date date9.;
datalines;
AAA 1 20-Dec-18
AAA 2 21-Dec-18
AAA 3 24-Dec-18
AAA 1 05-Jan-19
AAA 3 06-Jan-19
BBB 1 20-Dec-18
BBB 2 21-Dec-18
;

data _null_;
   if _N_ = 1 then do;
      declare hash h(ordered:'A');
      h.defineKey('Client', 'Business_Stage');
      h.defineData('Client', 'Business_Stage', 'time_spent');
      h.defineDone();
   end;

   merge have have(firstobs=2 rename=(client=client2 date=date2) drop=Business_Stage) end=lr;
   if client2 ne client then time_spent=0;
   else time_spent=intck('day', date, date2);

   if h.check() ne 0 then h.add();
   else do;
      _time_spent=time_spent;
      rc=h.find();
      time_spent=time_spent+_time_spent;
      h.replace();
   end;

   if lr then h.output(dataset:'want(where=(time_spent ne 0))');
run;

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 3 replies
  • 598 views
  • 1 like
  • 3 in conversation