Announcement

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

  • Creating lagged variable after tsset or xtset data error

    I am trying to create lagged "Total Assets" and went through few of the forum posts and instruction to tsset or xtset my data first. Since my data is panel data I used xtset on Year variable but running into error.

    The exact steps is used are:

    xtset Year >> gen Lagasset = l1.TotalAssets

    Any help would be appreciated.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str9 Ticker int Year long TotalAssets
    "TH:2S"  1993        .
    "TH:2S"  1994        .
    "TH:2S"  1995        .
    "TH:2S"  1996        .
    "TH:2S"  1997        .
    "TH:2S"  1998        .
    "TH:2S"  1999        .
    "TH:2S"  2000        .
    "TH:2S"  2001        .
    "TH:2S"  2002        .
    "TH:2S"  2003        .
    "TH:2S"  2004        .
    "TH:2S"  2005        .
    "TH:2S"  2006   626450
    "TH:2S"  2007   739620
    "TH:2S"  2008   559340
    "TH:2S"  2009   913418
    "TH:2S"  2010  1024697
    "TH:2S"  2011  1059772
    "TH:2S"  2012  1253012
    "TH:2S"  2013  1219920
    "TH:2S"  2014  1205561
    "TH:7UP" 1993        .
    "TH:7UP" 1994        .
    "TH:7UP" 1995        .
    "TH:7UP" 1996        .
    "TH:7UP" 1997        .
    "TH:7UP" 1998        .
    "TH:7UP" 1999        .
    "TH:7UP" 2000        .
    "TH:7UP" 2001  1239837
    "TH:7UP" 2002  1445609
    "TH:7UP" 2003  1589719
    "TH:7UP" 2004  2561613
    "TH:7UP" 2005  3659381
    "TH:7UP" 2006  4047774
    "TH:7UP" 2007  3776231
    "TH:7UP" 2008  4178294
    "TH:7UP" 2009  4126859
    "TH:7UP" 2010  4403855
    "TH:7UP" 2011  4072216
    "TH:7UP" 2012  1211540
    "TH:7UP" 2013  1229760
    "TH:7UP" 2014   922593

  • #2
    Those aren't the exact steps. They can't be, because << >> isn't part of Stata syntax. Anyways, the bigger problem is that you appear to have repeated time values in your sample, and you don't specify what the panel variable is meant to be, thus, you can't make a lagged variable. And, you can't have l1.[varname], the L must be capitalized as far as I'm aware.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str9 Ticker int Year long TotalAssets
    "TH:2S"  1993        .
    "TH:2S"  1994        .
    "TH:2S"  1995        .
    "TH:2S"  1996        .
    "TH:2S"  1997        .
    "TH:2S"  1998        .
    "TH:2S"  1999        .
    "TH:2S"  2000        .
    "TH:2S"  2001        .
    "TH:2S"  2002        .
    "TH:2S"  2003        .
    "TH:2S"  2004        .
    "TH:2S"  2005        .
    "TH:2S"  2006   626450
    "TH:2S"  2007   739620
    "TH:2S"  2008   559340
    "TH:2S"  2009   913418
    "TH:2S"  2010  1024697
    "TH:2S"  2011  1059772
    "TH:2S"  2012  1253012
    "TH:2S"  2013  1219920
    "TH:2S"  2014  1205561
    "TH:7UP" 1993        .
    "TH:7UP" 1994        .
    "TH:7UP" 1995        .
    "TH:7UP" 1996        .
    "TH:7UP" 1997        .
    "TH:7UP" 1998        .
    "TH:7UP" 1999        .
    "TH:7UP" 2000        .
    "TH:7UP" 2001  1239837
    "TH:7UP" 2002  1445609
    "TH:7UP" 2003  1589719
    "TH:7UP" 2004  2561613
    "TH:7UP" 2005  3659381
    "TH:7UP" 2006  4047774
    "TH:7UP" 2007  3776231
    "TH:7UP" 2008  4178294
    "TH:7UP" 2009  4126859
    "TH:7UP" 2010  4403855
    "TH:7UP" 2011  4072216
    "TH:7UP" 2012  1211540
    "TH:7UP" 2013  1229760
    "TH:7UP" 2014   922593
    
    end
    cls
    
    br
    tsset Year, y 
    
    g Lagasset = L1.TotalAssets

    Comment


    • #3
      This works on my end.
      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input str9 Ticker int Year long TotalAssets
      "TH:2S"  1993        .
      "TH:2S"  1994        .
      "TH:2S"  1995        .
      "TH:2S"  1996        .
      "TH:2S"  1997        .
      "TH:2S"  1998        .
      "TH:2S"  1999        .
      "TH:2S"  2000        .
      "TH:2S"  2001        .
      "TH:2S"  2002        .
      "TH:2S"  2003        .
      "TH:2S"  2004        .
      "TH:2S"  2005        .
      "TH:2S"  2006   626450
      "TH:2S"  2007   739620
      "TH:2S"  2008   559340
      "TH:2S"  2009   913418
      "TH:2S"  2010  1024697
      "TH:2S"  2011  1059772
      "TH:2S"  2012  1253012
      "TH:2S"  2013  1219920
      "TH:2S"  2014  1205561
      "TH:7UP" 1993        .
      "TH:7UP" 1994        .
      "TH:7UP" 1995        .
      "TH:7UP" 1996        .
      "TH:7UP" 1997        .
      "TH:7UP" 1998        .
      "TH:7UP" 1999        .
      "TH:7UP" 2000        .
      "TH:7UP" 2001  1239837
      "TH:7UP" 2002  1445609
      "TH:7UP" 2003  1589719
      "TH:7UP" 2004  2561613
      "TH:7UP" 2005  3659381
      "TH:7UP" 2006  4047774
      "TH:7UP" 2007  3776231
      "TH:7UP" 2008  4178294
      "TH:7UP" 2009  4126859
      "TH:7UP" 2010  4403855
      "TH:7UP" 2011  4072216
      "TH:7UP" 2012  1211540
      "TH:7UP" 2013  1229760
      "TH:7UP" 2014   922593
      
      end
      cls
      
      egen id = group(Ticker)
      
      br
      xtset id Year, y 
      
      g Lagasset = L1.TotalAssets
      Note that I assume you want the ticker to be your panel variable.

      Comment


      • #4
        Jared Greathouse Thank you so much ! This worked perfectly. Your solution allowed me to understand how the syntax works.

        P.S. sorry for putting <<>> that was meant to be "next line of code", realized this created confusion.

        Comment


        • #5
          Quite alright, no harm done. Out of curiosity though, why even bother creating a lagged variable at all? This syntax
          Code:
          reg y /// y = outcome
          L1.x // L1 = 1 Lag
          is perfectly legal in Stata's time series operators. I don't know what you're using it for, but don't make another variable if you don't have to, just use the relevant time series operators and you can make your regression that way. Farhan Hasnat

          Comment


          • #6
            Click image for larger version

Name:	Screen Shot 2565-01-12 at 00.25.04.png
Views:	1
Size:	13.5 KB
ID:	1644659

            Essentially I am working to prepare all the variables for this equation before running it. So "A" is the Lagged Total assets, I will also need to create ("CHANGE IN S") and the "TA" denotes subtracting one variable from another (earnings - cashflow). My initial was to create all these variables then merge them together in a dataset and run the regression. Would be great if I could get your thoughts on this if I really should prepare individual variables this way.

            My data is panel data and the idea you mentioned earlier seems like might make my work easier.

            Thank you once again for your suggestions

            Comment


            • #7
              .

              Comment


              • #8
                Jared Greathouse Hi Jared ! A quick question since you know my data, I want to drop Tickers for the lagged variable with all missing values. I was wondering if you could refer any commands I could use to achieve this result. I understand that I would need to use an expressing with drop and if but couldn't really get my head around how to address this.

                Comment


                • #9
                  Farhan Hasnat Are you saying you want to drop any ticker that has a missing value now that you've lagged your variable?

                  Comment


                  • #10
                    Jared Greathouse Yes exactly, earlier I was cleaning all my data on excel and the condition was if any firm/ticker all all null values for all years I would drop them.

                    Sorry to bother with many questions and this question might be irrelevant, but I created the lag using excel as well and seems like "missing values" generated using excel is much lower than what Stata generated. I understand this might be due to various reason but yet wondering if there might any thoughts that might come to you mind.

                    Thank you once again for all your guidance as I push through Stata. Good day !

                    Comment

                    Working...
                    X