Longitudinal dataset: counting the number of repeats and storing the value in a variable

69 Views Asked by At

I have a longitudinal/long layout dataset in Stata in which RecordIDs are repeated/ I want to generate a new variable which shows the number of times each ID is repeated. How can I write the code?

I tried:

bysort RecordID : gen repeat_no = _n

but I'm getting the serial number of the times of repeats.

I want a variable to store the total number of times each ID is repeated (like shown in the image).enter image description here

1

There are 1 best solutions below

0
Nick Cox On BEST ANSWER
bysort RecordID : gen repeat_NO = _N

For documentation of these basic constructs, see e.g.

help _variables