Search code examples
statastata-macros

Stata: Creating Variables Based on local varlist


I have a data set that looks like this:

A B
0 1
0 1
0 1
1 0

I want to create new variables A't' and B't' for t=1,2,3 that give A and B values for the past 1,2 and 3 periods. I tried the following code but I get the error: "A invalid name.

local status A B
    foreach x of local status {
    forvalues t=1/3 {
        gen "`x'"`t'="`x'"[_n-`t'] if _n>`t'
    }
    }

And the outcome I would like to get is the following:

A B  A1 A2 A3 B1 B2 B3
0 1  .  .  .  .  .  .
1 0  0  .  .  1  .  .
0 1  1  0  .  0  1  .
1 0  0  1  0  1  0  1

Solution

  • This works:

    clear 
    input A B
    0 1
    0 1
    0 1
    1 0
    end 
    
    foreach x in A B { 
        forval t = 1/3 { 
            gen `x'`t' = `x'[_n-`t']
        }
    }
    

    Notes:

    1. Putting two variable names into a local only to take them out again does no harm, but is pointless otherwise.

    2. The double quotes are wrong in this context.

    3. The if qualifier would do no harm but you get the same result without it.

    Most crucially, experienced Stata users would not do this. The idea of values one previous, two previous, and so forth only makes sense if the observations are in time or another sequence order, in which case most analyses require an explicit time-like variable, say

    gen t = _n 
    

    after which you can go

    tsset t 
    

    and the lagged variables are then automatically available as L1.A L2.A L3.A and so forth.

    If your real data are panel or longitudinal data then you need an identifier as well as a time variable.