JCV
JCV

Reputation: 21

Generate Group ID with 2 conditions in Stata

I need to assign a unique ID to each person every 62 observations. In the example below, every row with siteid from 1 to 62 and visits=3 would refer to person 1, every row with siteid from 1 to 62 and visits=1 would refer to person 2, etc. I tried

  egen newid=group(siteid visits) 

and

  by siteid: gen uniqueid = 1 if _n==1
  replace uniqueid = sum(uniqueid)

but none of these yield the desired results.

siteid

1
2
3
4
...
62
1
2
3
4
...
62
1
2
3
4
...
62

and so on

visits

3
3
3
3
...
3
1
1
1
1
...
1 

and so on.

Upvotes: 1

Views: 8230

Answers (1)

Nick Cox
Nick Cox

Reputation: 37208

You just want the first 62 observations to be assigned 1, the next 62 to be assigned 2, and so forth. That is done by

egen uniqueid = seq(), block(62)

The important detail here is that this command makes no reference to any of the existing variables. Their precise values are irrelevant. But you could go

gen uniqueid = sum(siteid == 1) 

to achieve the same effect. You score 1 every time siteid is 1, getting blocks of 1 followed by 61 zeros, and their cumulative sum from sum() is then blocks of 1s, 2s, etc.

Upvotes: 1

Related Questions