Hi there,
I have a dataset that looks like this, where I have generated n and N by using:
bysort ID: gen n=_n
bysort ID: gen N=_N
What I now want to do is generate n and N the same way, but only if the dummy = 1.
I tried this command:
bysort ID: gen n=_n if Dummy ==1
bysort ID: gen N=_N if Dummy ==1
and it produces something like this (basically the same table but with missings where Dummy =0):
But what I really want is it to ignore the observations where Dummy = 0 and do the 'count' or labeling or whatever it is only on those where dummy =1.
So that it looks like this:
Could somebody advise how to do this?
Many thanks,
A
I have a dataset that looks like this, where I have generated n and N by using:
bysort ID: gen n=_n
bysort ID: gen N=_N
ID | Dummy | n | N |
1 | 0 | 1 | 4 |
1 | 0 | 2 | 4 |
1 | 1 | 3 | 4 |
1 | 1 | 4 | 4 |
2 | 0 | 1 | 1 |
3 | 1 | 1 | 1 |
4 | 1 | 1 | 3 |
4 | 0 | 2 | 3 |
4 | 1 | 3 | 3 |
I tried this command:
bysort ID: gen n=_n if Dummy ==1
bysort ID: gen N=_N if Dummy ==1
and it produces something like this (basically the same table but with missings where Dummy =0):
ID |
|
n | N | |
1 | 0 | . | . | |
1 | 0 | . | . | |
1 | 1 | 3 | 4 | |
1 | 1 | 4 | 4 | |
2 | 0 | . | . | |
3 | 1 | 1 | 1 | |
4 | 1 | 1 | 3 | |
4 | 0 | . | . | |
4 | 1 | 3 | 3 |
But what I really want is it to ignore the observations where Dummy = 0 and do the 'count' or labeling or whatever it is only on those where dummy =1.
So that it looks like this:
ID | Dummy | n | N |
1 | 0 | . | . |
1 | 0 | . | . |
1 | 1 | 1 | 2 |
1 | 1 | 2 | 2 |
2 | 0 | . | . |
3 | 1 | 1 | 1 |
4 | 1 | 1 | 2 |
4 | 0 | . | . |
4 | 1 | 2 | 2 |
Could somebody advise how to do this?
Many thanks,
A
Comment