I have a panel data and this table shows an annual transaction of certain fruits. Now, I try to generate a new dummy variable that can identify consumers who only buy fruit in the summer. In other words, people who purchase fruits both in summer and non-summer are excluded. Besides, people purchase fruits only in non-summer are excluded as well.
In this case, with few observations, I am able to know the person purchases fruits only in Summer whose ID = {C,E}.
ID | ID over time | Season (season=1 if summer; zero otherwise) | Person who purchases fruits only in Summer |
A | 1 | Summer | 0 |
A | 2 | Nonsummer | 0 |
B | 1 | Nonsummer | 0 |
C | 1 | Summer | 1 |
C | 2 | Summer | 1 |
C | 3 | Summer | 1 |
D | 1 | Nonsummer | 0 |
E | 1 | Summer | 1 |
F | 1 | Summer | 0 |
F | 2 | Nonsummer | 0 |
Code:
egen x=1 if season=1, by(id)
Code:
bysort id season: generate x = _n
0 Response to How does Stata identify and generate a categorical variable from a subgroup of entire observation?
Post a Comment