Question

Let us say my data set is

t id b c z
1 1  0 1 1
2 1  1 1 0
3 1  0 1 1
4 1  0 1 1
1 2  0 1 1
1 2  1 1 1

I want to create a dummy variable Dum1 where Dum1= 1 if variable z takes value of 1 within the next two periods after observing b =1 for each id.

For example, for id=1, b=1 at t=2 and since for id=1 z=1 at t=3 and t=4 the dummy should take a value of one.

If this can be done in SAS, that is also fine.

Was it helpful?

Solution

tsset id t 
gen dum = b == 1 & (F.z == 1 | F2.z == 1) 

or

gen dum = b == 1 & inlist(1, F.z, F2.z) 

So this is a Stata translation after setting up the data as panel data with a panel and time variable. The indicator (I advise against the term "dummy", often misunderstood as offensive) is

1 if and only if the present value of b is 1 and either the next value or the next-but-one value of z is 1

0 otherwise

EDIT: If "within the next two time periods" means "both" not "either" then the inlist() solution is invalid and the | should be &, as in @Maarten Buis's answer.

OTHER TIPS

Here is how I would do this in Stata:

// make some example data
clear
input ///
t id b c z
1 1  0 1 1
2 1  1 1 0
3 1  0 1 1
4 1  0 1 1
1 2  0 1 1
2 2  1 1 1
end

// start making the variable
tsset id t
gen byte dum1 = ///
 ( ( b == 1 ) & ( F1.z == 1 ) & ( F2.z == 1 ) )

I created the indicator variable (a term I prefer over dummy variable) by feeding it a logical statement, which Stata evaluates as 1 if "true" and 0 if "false". For more on that see here.

I used the time series operators F1. and F2. to get at the values in the next two periods.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top