Re: st: creating lag variables

 From "Oleksandr Talavera, BC" <[email protected]> To [email protected] Subject Re: st: creating lag variables Date Mon, 05 May 2003 01:57:38 -0400

```Hi,

Try the following:
*----------
egen type1 = group(type)
g newid = 10^6*type1+id
tsset newid year
g lag_choice=L.choice
sort id  year type
*--------------
The output is below.

Best,
Sasha

. keep id type choice year
. list

+---------------------------+
| id   type   choice   year |
|---------------------------|
1. |  1      a        1      1 |
2. |  1      b        0      1 |
3. |  1      c        0      1 |
4. |  1      d        0      1 |
5. |  1      a        0      2 |
|---------------------------|
6. |  1      b        0      2 |
7. |  1      c        1      2 |
8. |  1      d        0      2 |
+---------------------------+

. egen type1 = group(type)

. g newid = 10^6*type1+id

. tsset newid year
panel variable:  newid, 1000001 to 4000001
time variable:  year, 1 to 2

. g lag_choice=L.choice
(4 missing values generated)

. sort id  year type

. list

+--------------------------------------------------------+
| id   type   choice   year   type1     newid   lag_ch~e |
|--------------------------------------------------------|
1. |  1      a        1      1       1   1000001          . |
2. |  1      b        0      1       2   2000001          . |
3. |  1      c        0      1       3   3000001          . |
4. |  1      d        0      1       4   4000001          . |
5. |  1      a        0      2       1   1000001          1 |
|--------------------------------------------------------|
6. |  1      b        0      2       2   2000001          0 |
7. |  1      c        1      2       3   3000001          0 |
8. |  1      d        0      2       4   4000001          0 |
+--------------------------------------------------------+

Reka Sundaram-Stukel wrote:
```
Dear Statalist,

I am trying to create a lag y for data that looks like the following:

id type choice Year lag_choice
1 a 1 1 -
1 b 0 1 -
1 c 0 1 -
1 d 0 1 -
1 a 0 2 1
1 b 0 2 0
1 c 1 2 0
1 d 0 2 0

how do I generate lag_choice. I have tried

by id type: gen lag_choice=choice[_n-1] and various combinations of this quite unsuccessfully. Help please

Reka

Reka Sundaram-Stukel
320 Taylor Hall
Department of Agriculture And Applied Economics

*
* For searches and help try:
* http://www.stata.com/support/faqs/res/findit.html
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/

```
*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```