Okay - I realize this is probably extremely easy to do in SAS but I for the life cannot figure it out - everything I've tried has not worked the way I need it to and am at my wits end. Any help would be greatly appreciated.
Let's say I have the following dataset in my work direcotry that consist of the first name of a customer and their date of purchase:
First_Name | Date_of_Purchase |
---|---|
John | 01/01/2010 |
Jill | 01/01/2010 |
Beth | 01/01/2010 |
Will | 02/01/2010 |
Jen | 02/01/2010 |
Lori | 03/01/2010 |
Tom | 03/01/2010 |
Heidi | 04/01/2010 |
What I need to do is an incremental count based on the change in date_of_purchase starting with the value of 1, results of which is shown below. I've tried various do-loop statements but cannot seem to populate the variable count with the value when the date remains unchanged (for example, for 01/01/2010, I've no problem populating first.date_of_purchase with 1, but subsequent cells under Count tied to 01/01/2010 remain empty. I've about 3 years worth of purchase data, with over thousand of customers. Thanks! If any clarification is needed, don't hestitate to let me know.
First_Name | Date_of_Purchase | Count |
---|---|---|
John | 01/01/2010 | 1 |
Jill | 01/01/2010 | 1 |
Beth | 01/01/2010 | 1 |
Will | 02/01/2010 | 2 |
Jen | 02/01/2010 | 2 |
Lori | 03/01/2010 | 3 |
Tom | 03/01/2010 | 3 |
Heidi | 04/01/2010 | 4 |
Are you trying to increment based on number of purchases (which aren't shown in your sample data) or simply by number of months since 1JAN2010?
data want ;
set have ;
by Date_of_Purchase ;
Count + first.Date_of_Purchase ;
run ;
If the order were the same with the order at your post.
That will be easy.
data want;
set have;
if date_of _purchase ne lag(date_of_purchase) then count+1;
run;
Ksharp
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.