Dear Stata users,

I have an unbalanced panel dataset where I tag all lags for each firm/year pair (variables nLags in the code), which I use to generate a new variable whose value depends on the specific values of each lags. The problem is that I don't know how to specify the multiple if conditions since the number of conditions depend on the number of lags. For example, if the firm has 3 lags, I have to check that L1.gas==0, L2.gas==0 and L3.gas==0. If the firm only has one lag, I only want to check that L1.gas==0 and so on.

In the following code example, 7 is the maximum possible number of lags.

forvalues i = 1/7 {
replace fuelswitch_togas= 1 if gas > 0 & L1.gas-L`i'.gas == 0 & nLags == `i'
}

Thanks for your help,
Emmanuel Murray Leclair