Numbering Consecutive Observations

Reply
Occasional Contributor EB1
Occasional Contributor
Posts: 12

Numbering Consecutive Observations

Hi all, I am looking to code a variable indicating the observation number. I would like it to code the same observation number for consecutive datapoints, and when it encounters a non-consecutive datapoint to start a new observation number. The following is what I currently have:

 

ID          Time

001        1331

001        1332

001        1333

001        1334

003        1111

003        1112

003        1113

051        7898

051        7699

051        7670

 

And this is what I would like to get:

 

ID          Time         Obsn

001        1331          1

001        1332          1

001        1333          1

001        1334          1

003        1111          2

003        1112          2

003        1113          2

051        7898          3

051        7699          3

051        7670          3

 

Thank you for your help!

Super User
Posts: 5,509

Re: Numbering Consecutive Observations

Assuming your data is sorted by ID (sort it first if it isn't):

 

data want;

set have;

by id;

obsn + first.id;

run;

Super User
Posts: 19,815

Re: Numbering Consecutive Observations

I don't think your data illustrates your problem, at least the issues you've been having in previous threads.

 

Please take the time to generate issues that you're facing. 

 

Based on your word description, look into the dif or lag function that can take the difference between observations. If the difference is > X then you can reset the counter.

Ask a Question
Discussion stats
  • 2 replies
  • 245 views
  • 0 likes
  • 3 in conversation