BookmarkSubscribeRSS Feed
newsas007
Quartz | Level 8

Hi all,

Here is my dataset 

IDProduct_OrderProduct_Received
110/6/201810/23/2018
110/6/201810/31/2018
110/6/201812/3/2018
210/13/201811/1/2018
210/13/201812/18/2018
37/16/20167/26/2016
310/2/201710/22/2017
412/10/201612/27/2016
42/13/20183/5/2018
51/24/20172/12/2017
512/26/20171/12/2018
51/10/20182/20/2018
62/6/20183/10/2018
62/9/20183/10/2018
74/5/20185/5/2018
75/5/20186/4/2018
76/5/20187/5/2018
77/5/20188/4/2018

I would like to calculate the order event from these dates. See below:

Here are the rules:

If the product_order date is the same date for ID then the order_event is the same. For example, ID#1 has the same order dates so the order event is still 1.

Similary, if the product was received on the same day the order_even is the same. For example, ID#6 has the same product_received day so the order_event is still 1.

Notice for #3: the second and third order is the same date so the order number is 2. This will be the same for same product received dates

For the rest, different order and received dates are added as a count. 

This is the final data:

IDProduct_OrderProduct_ReceivedOrder_event
110/6/201810/23/20181
110/6/201810/31/20181
110/6/201812/3/20181
210/13/201811/1/20181
210/13/201812/18/20181
37/16/20167/26/20161
310/2/201710/22/20172
310/2/201710/14/20172
412/10/201612/27/20161
42/13/20183/5/20182
51/24/20172/12/20171
512/26/20171/12/20182
51/10/20182/20/20183
62/6/20183/10/20181
62/9/20183/10/20181
74/5/20185/5/20181
75/5/20186/4/20182
76/5/20187/5/20183
77/5/20188/4/20184
1 REPLY 1
smantha
Lapis Lazuli | Level 10
data have;
informat id 3. Product_Order mmddyy10. Product_Received mmddyy10.;
input id Product_Order Product_Received;
format id 3. Product_Order mmddyy10. Product_Received mmddyy10.;
cards;
1	10/6/2018	10/23/2018
1	10/6/2018	10/31/2018
1	10/6/2018	12/3/2018
2	10/13/2018	11/1/2018
2	10/13/2018	12/18/2018
3	7/16/2016	7/26/2016
3	10/2/2017	10/22/2017
4	12/10/2016	12/27/2016
4	2/13/2018	3/5/2018
5	1/24/2017	2/12/2017
5	12/26/2017	1/12/2018
5	1/10/2018	2/20/2018
6	2/6/2018	3/10/2018
6	2/9/2018	3/10/2018
7	4/5/2018	5/5/2018
7	5/5/2018	6/4/2018
7	6/5/2018	7/5/2018
7	7/5/2018	8/4/2018
;;;
run;
proc sort; by id Product_Order Product_Received;
run;
data want;
set have;
by id Product_Order Product_Received;
retain order_event prev_order prev_recv;
if first.id then do;
 order_event=1;
 prev_order=Product_Order;
 prev_recv=Product_Received;
end;
put _all_;
if (prev_order ^= Product_Order) and (prev_recv^=Product_Received) then order_event=order_event+1;
prev_order=Product_Order;
prev_recv=Product_Received;
run;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 1 reply
  • 702 views
  • 1 like
  • 2 in conversation