Reputation: 237
In a panel data set I have 3 variables: name
, week
, and income
.
I would like to make an indicator variable that indicates initial weeks where income
is 0. So say a person X has 0 income
in the first 13 weeks, the indicator takes the value 1 the first 13 weeks, and is otherwise 0. The same procedure for person Y and so on.
I have tried using by groups, but I can't get it to work.
Any suggestions?
Upvotes: 0
Views: 171
Reputation: 37208
One solution is
bysort name (week) : gen no_income = sum(income) == 0
The function sum()
yields cumulative or running sum. So, as long as income
is 0, its cumulative sum remains 0 too. As soon as a person earns something, the cumulative sum becomes positive. The code is based on the presumption that cumulative income can not cross zero again because in a given week, income
is negative. To exclude that possibility use an appropriate extra condition, such as
bysort name (week) : gen no_income = sum(income) == 0 & income == 0
For a problem with very similar flavour, see this FAQ. A meta-lesson is to look at the StataCorp FAQs as one of several resources.
Upvotes: 2