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

## Numbering the nth occurrence

Hi I was wondering how to order the following data into a new column.

ID      ORDERID  DATE

1000  1098          2018-05-22

1000  1756          2018-05-29

1000  1965          2018-07-22

1001  1145          2018-06-14

1001  1456          2018-08-12

1002  1468          2018-05-28

1003  1548          2018-09-03

1004  1007          2018-06-01

1004  1789          2018-07-27

What I need is a new purchase count column to tell me the 1st, 2nd etc purchase from each ID.

ID      ORDERID  DATE           No of purchase

1000  1098          2018-05-22     1

1000  1756          2018-05-29     2

1000  1965          2018-07-22     3

1001  1145          2018-06-14     1

1001  1456          2018-08-12     2

1002  1468          2018-05-28     1

1003  1548          2018-09-03     1

1004  1007          2018-06-01     1

1004  1789          2018-07-27     2

Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
Tourmaline | Level 20

## Re: Numbering the nth occurrence

``````data have;
input ID \$ ORDERID \$ DATE:yymmdd10.;
format DATE yymmdd10.;
datalines;
1000 1098 2018-05-22
1000 1756 2018-05-29
1000 1965 2018-07-22
1001 1145 2018-06-14
1001 1456 2018-08-12
1002 1468 2018-05-28
1003 1548 2018-09-03
1004 1007 2018-06-01
1004 1789 2018-07-27
;

data want;
set have;
by ID;
No_of_purchase+1;
if first.ID then No_of_purchase=1;
retain No_of_purchase;
run;``````
3 REPLIES 3
Jade | Level 19

## Re: Numbering the nth occurrence

Sort data by id, then use a datastep with group processing and retain/reset "NoOfPurchase".

If you want code, post data as datastep using datalines statement.

Tourmaline | Level 20

## Re: Numbering the nth occurrence

``````data have;
input ID \$ ORDERID \$ DATE:yymmdd10.;
format DATE yymmdd10.;
datalines;
1000 1098 2018-05-22
1000 1756 2018-05-29
1000 1965 2018-07-22
1001 1145 2018-06-14
1001 1456 2018-08-12
1002 1468 2018-05-28
1003 1548 2018-09-03
1004 1007 2018-06-01
1004 1789 2018-07-27
;

data want;
set have;
by ID;
No_of_purchase+1;
if first.ID then No_of_purchase=1;
retain No_of_purchase;
run;``````
Obsidian | Level 7

## Re: Numbering the nth occurrence

draycut - Ah, yes thank you! This is fantastic, just what I wanted! You are the most awesome person in the room today.
Discussion stats
• 3 replies
• 535 views
• 1 like
• 3 in conversation