Help using Base SAS procedures

Start an increment count based on changes in data values - how?

Reply
New Contributor
Posts: 3

Start an increment count based on changes in data values - how?

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

Beth01/01/2010
Will02/01/2010
Jen

02/01/2010

Lori03/01/2010
Tom03/01/2010
Heidi04/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_PurchaseCount
John

01/01/2010

1
Jill

01/01/2010

1
Beth01/01/20101
Will02/01/20102
Jen

02/01/2010

2
Lori03/01/20103
Tom03/01/20103
Heidi04/01/20104
PROC Star
Posts: 7,467

Start an increment count based on changes in data values - how?

Posted in reply to justchecking

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?

Regular Contributor
Posts: 184

Start an increment count based on changes in data values - how?

Posted in reply to justchecking

data want ;

set have ;

by Date_of_Purchase ;

Count + first.Date_of_Purchase ;

run ;

Super User
Posts: 10,020

Start an increment count based on changes in data values - how?

Posted in reply to justchecking

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

Ask a Question
Discussion stats
  • 3 replies
  • 151 views
  • 0 likes
  • 4 in conversation