Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Generating lagged variable for the groups in panel data

    Hello, I am trying to generate a new variable "z" that is the previous year's value of the variable "y" for every group of "x" as in the table.
    As you see in the table, it should be missing at years of 2000.
    I tried this code: bysort YEAR (x): egen z = l.y
    But it gave me "z" values for years of 2000 that is not sensible because we don't have info for the year 1999.
    Thanks




    YEAR x y z
    2000 1004 2.64
    2001 1004 -8.299 2.64
    2002 1004 -1.807 -8.299
    2003 1004 0.494 -1.807
    2004 1004 2.536 0.494
    2005 1004 3.592 2.536
    2000 1013 21.864
    2001 1013 -51.514 21.864
    2002 1013 -100.07 -51.514
    2003 1013 -5.914 -100.07
    2004 1013 2.192 -5.914
    2005 1013 5.57 2.192

  • #2
    Alper:
    welcome to this forum.
    As far as I can see your results (that you should have better reported via CODE delimiters, as per FAQ) the values for 2000 are missing.
    Kind regards,
    Carlo
    (Stata 19.0)

    Comment


    • #3
      Building on Carlo's reply, if you don't think the lagged value of y in 2000 should be the missing value (represented in Stata by a numeric value of . as described in the output of help missing) then what would you expect it to be, since you do not have data on the value of y in 1999?

      Comment


      • #4
        I thought perhaps I had misunderstood the example data in post #1: maybe that displayed what is wanted rather than what the code produced. The example below shows three ways of getting what you want; I note that the code you show in post #1 produces an error message and does not create the variable z.
        Code:
        * Example generated by -dataex-. For more info, type help dataex
        clear
        input int(YEAR x) float y
        2000 1004    2.64
        2001 1004  -8.299
        2002 1004  -1.807
        2003 1004    .494
        2004 1004   2.536
        2005 1004   3.592
        2000 1013  21.864
        2001 1013 -51.514
        2002 1013 -100.07
        2003 1013  -5.914
        2004 1013   2.192
        2005 1013    5.57
        end
        xtset x YEAR
        generate z1 = l.y
        bysort x (YEAR): generate z2 = l.y
        bysort x (YEAR): generate z3 = y[_n-1]
        list, clean
        Code:
        . list, clean
        
               YEAR      x         y        z1        z2        z3  
          1.   2000   1004      2.64         .         .         .  
          2.   2001   1004    -8.299      2.64      2.64      2.64  
          3.   2002   1004    -1.807    -8.299    -8.299    -8.299  
          4.   2003   1004      .494    -1.807    -1.807    -1.807  
          5.   2004   1004     2.536      .494      .494      .494  
          6.   2005   1004     3.592     2.536     2.536     2.536  
          7.   2000   1013    21.864         .         .         .  
          8.   2001   1013   -51.514    21.864    21.864    21.864  
          9.   2002   1013   -100.07   -51.514   -51.514   -51.514  
         10.   2003   1013    -5.914   -100.07   -100.07   -100.07  
         11.   2004   1013     2.192    -5.914    -5.914    -5.914  
         12.   2005   1013      5.57     2.192     2.192     2.192
        To improve your future posts to Statalist, please take a few moments to review the Statalist FAQ linked to from the top of the page, as well as from the Advice on Posting link on the page you used to create your post. Note especially sections 9-12 on how to best pose your question. It is particularly helpful to copy commands and output from your Stata Results window and paste them into your Statalist post using code delimiters [CODE] and [/CODE], and to use the dataex command to provide sample data, as described in section 12 of the FAQ.

        Comment

        Working...
        X