1
votes

I have a Stata data set like this:

HouseholdId  PersonId OtherVariables
1               1
1               2                 
2               1
2               2
3               1
3               2

Here HouseholdId is a unique identifier for each household and PersonId is a unique identifier for each person in a household. If I want to create a unique personal id for each person within the sample, period. How would I do this?

I have tried egen per_id = group(PersonID HouseholdID) but that doesn't seem to work.

1
I've made some edits correcting inconsistencies in variable names and some other details. If I have changed your intent you will need to edit yourself. - Nick Cox
"doesn't seem to work" is never a good problem report. Why "seem"? What happened? Why is it wrong? Why is it not what you want? - Nick Cox

1 Answers

1
votes

I take it that you want a unique identifier for each person within the entire dataset. That could be just

sort HouseholdId PersonId 
gen long obs Id = _n 

as follows from an accessible discussion in this Stata FAQ. That would have been found by typing in Stata

search identifier

or even

search id 

(Meta-answer: You can and should look within Stata for information on basic notions like this.)

I add a strong recommendation that the word unique still carries its original meaning of appearing once only. The word distinct is, I suggest, a much better word when that is what you mean. More on that on p.558 of this paper.