Announcement

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

  • Create lagged variables

    Hi everybody, i'm Andrea and for my university thesis i'm using STATA12.
    I'm currently working on a panel data but i'm not able to create lagged variables.For example i need to create tha variable called #employees (t-1)
    Could you help me?
    PS: the data i have are similar to these ones
    Attached Files

  • #2
    You need to -xtset- your data in Stata, and then you can use the L1 operator to refer to the 1 time period lag, L1.employees, for example.

    Please don't show data by attaching Excel spreadsheets. Some members of the list, including some of the most frequent responders, do not have or use Microsoft Office products. Even those who do may be reluctant to open up a spreadsheet provided by a stranger, given that these files can contain active content that can damage a computer.

    Finally, please note the requests that posters use their real first and last name as their user ID. You can change your registration by pressing the Contact Us button below.

    Comment


    • #3
      sorry, i'm new to the dorum and i didn't know that

      Comment


      • #4
        Welcome to the forum, and thanks for changing your Forum login name as Clyde suggested. You said
        i didn't know that
        . May I suggest that you quickly read through the one-page Forum FAQ? There's a clickable tab at the top of the page. Lots of useful stuff in there to help maximize the chances of you getting a helpful answer to questions posted. As for your substantive question, and as Clyde suggested, see help xtset, and also look at manual [XT] -- this has lots in it about panel data organisation and management

        Comment


        • #5
          Dear statalist
          I am working on a panel data set and i need to create some lagged variables.
          However, when i try doing this in stata 14 using the code: gen lrainfall = l.rainfall, i get missing values for all my observations. What is happening?
          Can someone help?

          Comment


          • #6
            Without seeing your data it is hard to be sure. But if your data are at, say, 5 year intervals, and you have just done -xtset location year-, Stata will assume that you have yearly data. When you specific l.rainfall, it tries to find the rainfall from 1 year before, but, of course, it doesn't exist, so it returns a missing value. If this is your problem, you need to specify the -interval(5)- observation in your -xtset- command, so Stata will know that "consecutive" observations mean 5 years apart.

            If this isn't your problem, or if you don't see how it applies to you, I suggest posting back with some example data. Be sure to use the -dataex- command to do that. See FAQ #12 if you are not familiar with -dataex-.

            Comment


            • #7
              Thanks very much Clyde
              Where exactly do I put in the interval(year) command?. I feel my solution lies here.

              Also going through the dataex command, I get errors when I run it

              Comment


              • #8
                So, if your data are at 5-year intervals, it's -xtset panel_variable_name year, interval(5)-. Read -help xtset- to get a grasp of this all-important command.

                Also going through the dataex command, I get errors when I run it
                What errors are you getting? Show the command you issued and the output you got from Stata, exactly without any modifications.

                Comment


                • #9
                  good morning Clyde,

                  When i run this command : xtset qnno year, interval(3)
                  i obtain this error
                  'option interval() not allowed'

                  With dataex, when i run 'dataex xtset qnno year, yearly' or 'dataex make gen lrainfall = l.rainfall'
                  I get variable xtset or make not found

                  Comment


                  • #10
                    When i run this command : xtset qnno year, interval(3)
                    i obtain this error
                    'option interval() not allowed'
                    My mistake. It's -delta(3)-, not -interval(3)-. Sorry for leading you down the wrong path.

                    [quote]With dataex, when i run 'dataex xtset qnno year, yearly' or 'dataex make gen lrainfall = l.rainfall'
                    I get variable xtset or make not found[/quote[
                    Your mistake. That's not what -dataex- is for. -dataex- is used for the sole purpose of posting an example of Stata data in this forum. Placing Stata commands after -dataex- will just get you error messages, as you have discovered. -dataex- is not used to show commands or Stata output, only Stata data. Re-read the instructions at -help dataex- and you will find that it is very easy to use for its intended purpose.

                    Comment


                    • #11
                      Thanks very much Clyde for your patience on this.
                      When I run with delta(3), i get 'time values with period less than delta() found'

                      Here is my data set
                      dataex Price_improved rainfall qnno year

                      Comment


                      • #12
                        Martin, there was no data set. Just typing -dataex Price_improved rainfall qnno year- in the Forum editor doesn't do it. Let's go through the steps carefully:

                        1. Launch Stata and open your data set.
                        2. In Stata's command window type
                        Code:
                        dataex Price_improved rainfall qnno year
                        Stata will put some output in the Results window.

                        3. The first line of output is a bunch of dashes, then "copy starting from the next line" then some more dashes. Ignore that line. The next line begins with [. Starting with that [ use your mouse to highlight all the remaining output you got from -dataex- until you hit a line with a bunch of dashes surrounding "copy up to and including the previous line". Don't highlight the line with the dashes, but everything up to that. Now copy that to the clipboard.

                        4. Go to the Forum and in the editor window for your response, paste what you had in your clipboard. That's it. You will see something in the Forum editor that looks more or less like the -dataex- output except that it is poorly aligned because it's in a variable-width type-face. That's OK. Leave it a lone. Don't edit it in any way. Just post it like that.

                        Comment


                        • #13
                          Thanks Clyde
                          Hope I got it now?
                          Code:
                          * Example generated by -dataex-. To install: ssc install dataex
                          clear
                          input int(qnno year) float(Price_improved rainfall)
                           1 2008 1.89  696.3611
                           1 2010 1.89  720.1329
                           1 2014 1.89  760.3068
                           2 2008 1.89  897.0068
                           2 2010 1.89  648.2111
                           2 2014 1.89  728.1722
                           3 2008 1.89 583.82043
                           3 2010 1.89 568.52997
                           3 2014 1.89  770.0025
                           4 2008 1.89  762.8637
                           4 2010 1.89  742.3915
                           4 2014 1.89  641.1036
                           5 2008 1.89  885.8186
                           5 2010 1.89  795.8561
                           5 2014 1.89  760.3068
                           6 2008 1.89  924.7388
                           6 2010 1.89  756.6147
                           6 2014 1.89  682.6801
                           7 2008 1.89  710.8336
                           7 2010 1.89 558.08716
                           7 2014 1.89  728.1722
                           8 2008 1.89  639.6596
                           8 2010 1.89 597.99457
                           8 2014 1.89  640.0641
                           9 2008 1.89  701.9286
                           9 2010 1.89  725.7533
                           9 2014 1.89  584.5856
                          10 2008 1.89  698.5183
                          10 2010 1.89  702.5728
                          10 2014 1.89  744.6637
                          11 2008 1.89  924.7388
                          11 2010 1.89  767.3892
                          11 2014 1.89   694.672
                          12 2008 1.89  821.9679
                          12 2010 1.89  781.3117
                          12 2014 1.89  620.2366
                          13 2008 1.89  639.6596
                          13 2010 1.89  795.9692
                          13 2014 1.89  620.2366
                          14 2008 1.89  845.4444
                          14 2010 1.89  648.4041
                          14 2014 1.89  714.8718
                          15 2008 1.89 579.16113
                          15 2010 1.89  767.3892
                          15 2014 1.89  706.0712
                          16 2008 1.89  625.7332
                          16 2010 1.89  721.0557
                          16 2014 1.89  770.0025
                          17 2008 1.89  698.5183
                          17 2010 1.89  736.4758
                          17 2014 1.89  640.0641
                          19 2008 1.89   889.875
                          19 2010 1.89 597.99457
                          19 2014 1.89  770.0025
                          20 2008 1.89  667.0734
                          20 2010 1.89  756.6147
                          20 2014 1.89  640.0641
                          21 2008 1.89  701.9286
                          21 2010 1.89  720.1329
                          21 2014 1.89  714.8718
                          22 2008 1.89  696.3611
                          22 2010 1.89   600.208
                          22 2014 1.89   694.672
                          23 2008 1.89  762.8637
                          23 2010 1.89  736.4758
                          23 2014 1.89  682.5488
                          24 2008 1.89  801.5007
                          24 2010 1.89  782.1171
                          24 2014 1.89  706.0712
                          25 2008 1.89  785.9922
                          25 2010 1.89  781.3117
                          25 2014 1.89  584.5856
                          26 2008 1.89  701.9286
                          26 2010 1.89  714.2349
                          26 2014 1.89   694.672
                          28 2008 1.89 579.16113
                          28 2010 1.89  832.4967
                          28 2014 1.89  770.0025
                          29 2008 1.89  698.5183
                          29 2010 1.89  795.9692
                          29 2014 1.89  604.3025
                          30 2008 1.89  622.4151
                          30 2010 1.89  720.1329
                          30 2014 1.89  760.3068
                          31 2008 1.89  871.4896
                          31 2010 1.89  742.3915
                          31 2014 1.89  620.0734
                          32 2008 1.89   889.875
                          32 2010 1.89  768.7584
                          32 2014 1.89  652.8281
                          34 2008 1.89  622.4151
                          34 2010 1.89  832.4967
                          34 2014 1.89   694.672
                          35 2008 1.89 579.16113
                          35 2010 1.89 568.52997
                          35 2014 1.89  682.6801
                          36 2008 1.89  667.0734
                          36 2010 1.89  648.4041
                          36 2014 1.89  714.8718
                          37 2008 1.89  639.6596
                          end
                          format %ty year
                          ------------------ copy up to and including the previous line ------------------

                          Listed 100 out of 1842 observations
                          Use the count() option to list more

                          Comment


                          • #14
                            Yes, you've got it now.

                            So this data isn't quite what you described it as. It does not have any regular time interval. Instead, there are three years, 2008, 2010, and 2014. So the gap between 2008 and 2010 is 2 years, and then the gap from 2014 to 2010 is four years. That averages out to a three year interval, but there aren't any actual three year intervals at all.

                            Now, this whole thread started because you were trying to get lagged values of rainfall and Stata was giving you all missing values. It is as I said originally: with -xtset qnno year-, Stata will interpret the lagged value to mean the value from the year before, and there is never any such observation in your data: it's always either 2 years or 4 years before. The -delta- option won't rescue us because there is no regular interval we can tell Stata to use. In the strict mathematical sense of the term, there are no lagged values in this data set.

                            So the first question is, is this data set correct. Why are there no 2012 values? Are they supposed to be there but somehow got lost during the creation of the data set? If so, then the solution is to get a corrected data set that includes 2012 data in addition to what you have. If you do that, you will be able to -xtset qnno year, delta(2)- and you won't have any problem with that. And the lag operators will work properly.

                            If there isn't supposed to be any 2012 data, or you just can't get your hands on it, perhaps you want to use a pseudo-lag: where 2010 is the "lagged" year for 2014 and 2008 is the "lagged" year for 2010.. In that case you have to create a different time variable that treats 2010 as the successor to 2008 and 2014 as the successor to 2010. I would do this as follows:

                            Code:
                            gen int sequential = 1 if year == 2008
                            replace sequential = 2 if year == 2010
                            replace sequential = 3 if year == 2014
                            xtset qnno sequential
                            Now if you use lag operators, the lag is based on the variable sequential, so that, in effect, 2010 is the lag of 2014 and 2008 is the lag of 2010.

                            Comment


                            • #15
                              Thanks very much Clyde
                              I just ran the code and it worked but when I run 'gen variable=l1.variable', I get 614 missing cases.
                              When i run 'gen variable=l0.variable', I get no missing values.
                              Am I intuitively right to run the latter code?

                              Also, the lagged values are hardly different from the real values. What is your advice please?

                              Comment

                              Working...
                              X