BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
nab102
Fluorite | Level 6

Hi ,

This is the dataset I have . I have one date of which a book was purchased. Each customer has a unique Id (shown as customer_id below) . So for each customer have a purchase date. I want to separate the purchase date into three separate group. 

1.PNG

 

This is what I want. For each customer Id, the first date is the same as the purchase_date, so everyone has a purchase date. And their subsequent dates after the purchase is recording in second date and third if they have any. So for example custome_id T002 made purchases twice, one on 09/05/2019 which will be the first date and the second purchase was on 05/07/2021 that's the second date . Since this particular customer doesn't make a third purchase, third_date is recorded as NA.

2.PNG

 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

1. Sort the data by Customer ID and Purchase Date

proc sort data=have;
by Customer_ID purchase_date;
run;

2. Add a number to indicate the purchase order

data purchaseOrder;
set have;
by Customer_ID purchase_date;
if first.customer_id then order=1;
else order+1;
run;

3. Transpose it to a wide format

proc transpose data=purchaseOrder out=purchaseWide prefix=Purchase;
by CustomerID;
id order;
var purchase_date;
run;

Your columns will be called Purchase1, Purchase2, Purchase3 instead of first_date, second_date, etc.

 


@nab102 wrote:

Hi ,

This is the dataset I have . I have one date of which a book was purchased. Each customer has a unique Id (shown as customer_id below) . So for each customer have a purchase date. I want to separate the purchase date into three separate group. 

1.PNG

 

This is what I want. For each customer Id, the first date is the same as the purchase_date, so everyone has a purchase date. And their subsequent dates after the purchase is recording in second date and third if they have any. So for example custome_id T002 made purchases twice, one on 09/05/2019 which will be the first date and the second purchase was on 05/07/2021 that's the second date . Since this particular customer doesn't make a third purchase, third_date is recorded as NA.

2.PNG

 


 

View solution in original post

5 REPLIES 5
PaigeMiller
Diamond | Level 26

How is it that customer T007 has no 2nd date but does have a 3rd date? That seems illogical.

 

 

--
Paige Miller
Reeza
Super User
Realizing this doesn't match your output. You have your lines repeated, I only have the final line so this may not be what you need.
Reeza
Super User

1. Sort the data by Customer ID and Purchase Date

proc sort data=have;
by Customer_ID purchase_date;
run;

2. Add a number to indicate the purchase order

data purchaseOrder;
set have;
by Customer_ID purchase_date;
if first.customer_id then order=1;
else order+1;
run;

3. Transpose it to a wide format

proc transpose data=purchaseOrder out=purchaseWide prefix=Purchase;
by CustomerID;
id order;
var purchase_date;
run;

Your columns will be called Purchase1, Purchase2, Purchase3 instead of first_date, second_date, etc.

 


@nab102 wrote:

Hi ,

This is the dataset I have . I have one date of which a book was purchased. Each customer has a unique Id (shown as customer_id below) . So for each customer have a purchase date. I want to separate the purchase date into three separate group. 

1.PNG

 

This is what I want. For each customer Id, the first date is the same as the purchase_date, so everyone has a purchase date. And their subsequent dates after the purchase is recording in second date and third if they have any. So for example custome_id T002 made purchases twice, one on 09/05/2019 which will be the first date and the second purchase was on 05/07/2021 that's the second date . Since this particular customer doesn't make a third purchase, third_date is recorded as NA.

2.PNG

 


 

Tom
Super User Tom
Super User

You really don't need to add the ORDER variable. 

PROC TRANSPOSE will  number the variables sequentially automatically.

proc transpose data=have out=purchaseWide prefix=Purchase;
  by CustomerID;
  var purchase_date;
run;

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
  • 544 views
  • 0 likes
  • 4 in conversation