Search code examples
statalongitudinallong-format-data

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


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


Solution

  • bysort RecordID : gen repeat_NO = _N
    

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

    help _variables