Announcement

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

  • Generating Lag Values

    Hi All,

    I have panel data, and I am trying to generate a lag value of a variable.

    I used the following command:
    "xtset co_code wdatef"
    " gen L1_bse_closing_price = L.bse_closing_price"

    However, dots appear in the lag values.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input long co_code str66 company_name str10 co_stkdate str11 bse_opening_price double bse_closing_price str8 bse_returns str13 bse_market_cap byte(date2 month) int cal_year float(f_year ddate ym wdatef L_bse_closing_price)
    11 "20 MICRONS LTD." "06-10-2008" "50.0000"  33.3 "1.0000" "470.2100"  6 10 2008 2009 17811 585 17811 .
    11 "20 MICRONS LTD." "13-10-2008" "24.0000"  24.9 "1.0700" "351.5984" 13 10 2008 2009 17818 585 17818 .
    11 "20 MICRONS LTD." "20-10-2008" "22.9000" 22.05 "0.9900" "311.3552" 20 10 2008 2009 17825 585 17825 .
    11 "20 MICRONS LTD." "27-10-2008" "15.9000"  15.1 "0.9200" "213.2183" 27 10 2008 2009 17832 585 17832 .
    11 "20 MICRONS LTD." "03-11-2008" "17.5000"    17 "1.0100" "240.0471"  3 11 2008 2009 17839 586 17839 .
    11 "20 MICRONS LTD." "10-11-2008" "21.0000"  19.8 "0.9700" "279.5843" 10 11 2008 2009 17846 586 17846 .
    11 "20 MICRONS LTD." "17-11-2008" "18.5000" 17.85 "0.9600" "252.0495" 17 11 2008 2009 17853 586 17853 .
    11 "20 MICRONS LTD." "24-11-2008" "16.0000" 16.55 "1.0300" "233.6929" 24 11 2008 2009 17860 586 17860 .
    11 "20 MICRONS LTD." "01-12-2008" "16.2000" 16.25 "0.9800" "229.4568"  1 12 2008 2009 17867 587 17867 .
    11 "20 MICRONS LTD." "08-12-2008" "16.9000" 16.35 "1.0100" "230.8689"  8 12 2008 2009 17874 587 17874 .
    11 "20 MICRONS LTD." "15-12-2008" "16.6000" 17.25 "1.0100" "243.5772" 15 12 2008 2009 17881 587 17881 .
    11 "20 MICRONS LTD." "22-12-2008" "17.0000" 16.75 "1.0300" "236.5170" 22 12 2008 2009 17888 587 17888 .
    11 "20 MICRONS LTD." "29-12-2008" "15.5500"  15.7 "1.0000" "221.6906" 29 12 2008 2009 17895 587 17895 .
    11 "20 MICRONS LTD." "05-01-2009" "19.0000"  18.7 "1.0100" "264.0518"  5  1 2009 2009 17902 588 17902 .
    11 "20 MICRONS LTD." "12-01-2009" "14.5000"  14.6 "0.9700" "206.1581" 12  1 2009 2009 17909 588 17909 .
    11 "20 MICRONS LTD." "19-01-2009" "14.6000" 14.45 "1.0200" "204.0401" 19  1 2009 2009 17916 588 17916 .
    11 "20 MICRONS LTD." "27-01-2009" "14.3000" 14.15 "0.9900" "199.8039" 27  1 2009 2009 17924 588 17923 .
    11 "20 MICRONS LTD." "02-02-2009" "15.0000" 14.94 "1.0000" "210.9591"  2  2 2009 2009 17930 589 17930 .
    11 "20 MICRONS LTD." "09-02-2009" "16.4000" 16.37 "1.0500" "231.1513"  9  2 2009 2009 17937 589 17937 .
    11 "20 MICRONS LTD." "16-02-2009" "18.0000" 16.29 "0.9400" "230.0216" 16  2 2009 2009 17944 589 17944 .
    11 "20 MICRONS LTD." "24-02-2009" "15.5500" 15.14 "0.9400" "215.0675" 24  2 2009 2009 17952 589 17951 .
    11 "20 MICRONS LTD." "02-03-2009" "16.4500"  15.9 "1.0100" "225.8634"  2  3 2009 2009 17958 590 17958 .
    11 "20 MICRONS LTD." "09-03-2009" "14.6000" 14.35 "0.9600" "203.8453"  9  3 2009 2009 17965 590 17965 .
    11 "20 MICRONS LTD." "16-03-2009" "15.5000"  15.1 "1.0100" "214.4992" 16  3 2009 2009 17972 590 17972 .
    11 "20 MICRONS LTD." "23-03-2009" "15.5000"  15.5 "1.0400" "220.1813" 23  3 2009 2009 17979 590 17979 .
    11 "20 MICRONS LTD." "30-03-2009" "15.2000" 14.85 "1.0100" "210.9479" 30  3 2009 2009 17986 590 17986 .
    11 "20 MICRONS LTD." "06-04-2009" "16.6000" 15.89 "0.9900" "225.7214"  6  4 2009 2010 17993 591 17993 .
    11 "20 MICRONS LTD." "13-04-2009" "17.3500" 18.94 "1.1000" "269.0474" 13  4 2009 2010 18000 591 18000 .
    11 "20 MICRONS LTD." "20-04-2009" "18.2500" 17.47 "1.0200" "248.1657" 20  4 2009 2010 18007 591 18007 .
    11 "20 MICRONS LTD." "27-04-2009" "21.5500" 21.75 "0.9800" "308.9641" 27  4 2009 2010 18014 591 18014 .
    11 "20 MICRONS LTD." "04-05-2009" "24.5000"  22.6 "0.9700" "321.0386"  4  5 2009 2010 18021 592 18021 .
    11 "20 MICRONS LTD." "11-05-2009" "22.4500"  20.8 "0.9600" "295.4692" 11  5 2009 2010 18028 592 18028 .
    11 "20 MICRONS LTD." "19-05-2009" "22.1500"  21.2 "1.0500" "301.1513" 19  5 2009 2010 18036 592 18035 .
    11 "20 MICRONS LTD." "25-05-2009" "25.4500"  26.4 "1.0900" "375.0185" 25  5 2009 2010 18042 592 18042 .
    11 "20 MICRONS LTD." "01-06-2009" "24.5000"  25.9 "1.0500" "367.9159"  1  6 2009 2010 18049 593 18049 .
    11 "20 MICRONS LTD." "08-06-2009" "30.0000"    28 "0.9500" "397.7469"  8  6 2009 2010 18056 593 18056 .
    11 "20 MICRONS LTD." "15-06-2009" "26.0000" 24.45 "0.9600" "347.3183" 15  6 2009 2010 18063 593 18063 .
    11 "20 MICRONS LTD." "22-06-2009" "23.3000"  23.5 "1.0000" "333.8233" 22  6 2009 2010 18070 593 18070 .
    11 "20 MICRONS LTD." "29-06-2009" "23.1000" 23.65 "1.0100" "335.9541" 29  6 2009 2010 18077 593 18077 .
    11 "20 MICRONS LTD." "06-07-2009" "24.5000" 23.65 "0.9600" "335.9541"  6  7 2009 2010 18084 594 18084 .
    11 "20 MICRONS LTD." "13-07-2009" "22.2500" 21.75 "0.9500" "308.9641" 13  7 2009 2010 18091 594 18091 .
    11 "20 MICRONS LTD." "20-07-2009" "22.5000" 23.05 "1.0000" "327.4310" 20  7 2009 2010 18098 594 18098 .
    11 "20 MICRONS LTD." "27-07-2009" "25.4500" 26.15 "1.1000" "371.4672" 27  7 2009 2010 18105 594 18105 .
    11 "20 MICRONS LTD." "03-08-2009" "25.9500" 26.05 "1.0100" "370.0467"  3  8 2009 2010 18112 595 18112 .
    11 "20 MICRONS LTD." "10-08-2009" "25.1500" 24.85 "0.9700" "353.0004" 10  8 2009 2010 18119 595 18119 .
    11 "20 MICRONS LTD." "17-08-2009" "26.5000" 25.95 "0.9800" "368.6262" 17  8 2009 2010 18126 595 18126 .
    11 "20 MICRONS LTD." "24-08-2009" "33.6500"  31.6 "1.0300" "448.8858" 24  8 2009 2010 18133 595 18133 .
    11 "20 MICRONS LTD." "31-08-2009" "34.0000" 35.35 "1.1000" "502.1555" 31  8 2009 2010 18140 595 18140 .
    11 "20 MICRONS LTD." "07-09-2009" "44.6000"  44.6 "1.0500" "639.1638"  7  9 2009 2010 18147 596 18147 .
    11 "20 MICRONS LTD." "14-09-2009" "38.8000"  39.8 "1.0500" "570.3749" 14  9 2009 2010 18154 596 18154 .
    11 "20 MICRONS LTD." "22-09-2009" "43.4000"  42.8 "1.0000" "613.3680" 22  9 2009 2010 18162 596 18161 .
    11 "20 MICRONS LTD." "29-09-2009" "43.9000"  42.1 "0.9800" "603.3363" 29  9 2009 2010 18169 596 18168 .
    11 "20 MICRONS LTD." "05-10-2009" "40.0000" 39.05 "0.9500" "559.6266"  5 10 2009 2010 18175 597 18175 .
    11 "20 MICRONS LTD." "12-10-2009" "38.0000"  39.2 "1.0200" "561.7763" 12 10 2009 2010 18182 597 18182 .
    11 "20 MICRONS LTD." "20-10-2009" "40.7500" 41.45 "1.0400" "594.0211" 20 10 2009 2010 18190 597 18189 .
    11 "20 MICRONS LTD." "26-10-2009" "39.2000" 39.15 "1.0000" "561.0597" 26 10 2009 2010 18196 597 18196 .
    11 "20 MICRONS LTD." "03-11-2009" "37.7500"  37.7 "0.9600" "540.2798"  3 11 2009 2010 18204 598 18203 .
    11 "20 MICRONS LTD." "09-11-2009" "39.0000"  38.1 "1.0100" "546.0122"  9 11 2009 2010 18210 598 18210 .
    11 "20 MICRONS LTD." "16-11-2009" "44.6000" 42.55 "1.0000" "609.7852" 16 11 2009 2010 18217 598 18217 .
    11 "20 MICRONS LTD." "23-11-2009" "43.5500" 42.05 "0.9800" "602.6197" 23 11 2009 2010 18224 598 18224 .
    11 "20 MICRONS LTD." "30-11-2009" "41.7000" 43.85 "1.0500" "628.4156" 30 11 2009 2010 18231 598 18231 .
    11 "20 MICRONS LTD." "07-12-2009" "46.6000" 45.75 "0.9900" "655.6445"  7 12 2009 2010 18238 599 18238 .
    11 "20 MICRONS LTD." "14-12-2009" "46.4500" 45.85 "1.0000" "657.0776" 14 12 2009 2010 18245 599 18245 .
    11 "20 MICRONS LTD." "21-12-2009" "45.4000" 45.95 "1.0000" "658.5107" 21 12 2009 2010 18252 599 18252 .
    11 "20 MICRONS LTD." "29-12-2009" "47.9000"  46.4 "1.0000" "664.9597" 29 12 2009 2010 18260 599 18259 .
    11 "20 MICRONS LTD." "04-01-2010" "47.5500"  47.5 "1.0300" "680.7238"  4  1 2010 2010 18266 600 18266 .
    11 "20 MICRONS LTD." "11-01-2010" "50.0000" 50.95 "1.0400" "730.1659" 11  1 2010 2010 18273 600 18273 .
    11 "20 MICRONS LTD." "18-01-2010" "50.4000"  48.5 "0.9900" "695.0549" 18  1 2010 2010 18280 600 18280 .
    11 "20 MICRONS LTD." "25-01-2010" "46.4500" 46.15 "1.0100" "661.3769" 25  1 2010 2010 18287 600 18287 .
    11 "20 MICRONS LTD." "01-02-2010" "55.5500" 50.25 "0.9400" "720.1342"  1  2 2010 2010 18294 601 18294 .
    11 "20 MICRONS LTD." "08-02-2010" "46.5500" 44.55 "0.9900" "638.4473"  8  2 2010 2010 18301 601 18301 .
    11 "20 MICRONS LTD." "15-02-2010" "43.8000"    42 "0.9600" "601.9032" 15  2 2010 2010 18308 601 18308 .
    11 "20 MICRONS LTD." "22-02-2010" "45.0000" 41.15 "0.9700" "589.7218" 22  2 2010 2010 18315 601 18315 .
    11 "20 MICRONS LTD." "02-03-2010" "38.3000"  40.1 "1.0000" "574.6742"  2  3 2010 2010 18323 602 18322 .
    11 "20 MICRONS LTD." "08-03-2010" "42.0500" 43.45 "1.0500" "622.6832"  8  3 2010 2010 18329 602 18329 .
    11 "20 MICRONS LTD." "15-03-2010" "40.0000" 44.15 "1.1300" "632.7149" 15  3 2010 2010 18336 602 18336 .
    11 "20 MICRONS LTD." "22-03-2010" "40.3500" 41.55 "1.0000" "595.4542" 22  3 2010 2010 18343 602 18343 .
    11 "20 MICRONS LTD." "29-03-2010" "41.8000" 39.45 "1.0000" "565.3591" 29  3 2010 2010 18350 602 18350 .
    11 "20 MICRONS LTD." "05-04-2010" "40.5500"    42 "1.0400" "601.9032"  5  4 2010 2011 18357 603 18357 .
    11 "20 MICRONS LTD." "12-04-2010" "42.0000" 40.95 "1.0100" "586.8556" 12  4 2010 2011 18364 603 18364 .
    11 "20 MICRONS LTD." "19-04-2010" "41.9500"  40.2 "0.9800" "576.1073" 19  4 2010 2011 18371 603 18371 .
    11 "20 MICRONS LTD." "26-04-2010" "41.9500"  41.7 "1.0100" "597.6039" 26  4 2010 2011 18378 603 18378 .
    11 "20 MICRONS LTD." "03-05-2010" "41.9000"  40.6 "0.9900" "581.8397"  3  5 2010 2011 18385 604 18385 .
    11 "20 MICRONS LTD." "10-05-2010" "40.0000"  40.3 "1.0400" "577.5404" 10  5 2010 2011 18392 604 18392 .
    11 "20 MICRONS LTD." "17-05-2010" "44.5000"  37.6 "0.9800" "538.8467" 17  5 2010 2011 18399 604 18399 .
    11 "20 MICRONS LTD." "24-05-2010" "40.0000" 40.35 "1.0600" "578.2570" 24  5 2010 2011 18406 604 18406 .
    11 "20 MICRONS LTD." "31-05-2010" "40.5000" 41.75 "1.0300" "598.3204" 31  5 2010 2011 18413 604 18413 .
    11 "20 MICRONS LTD." "07-06-2010" "41.0000" 41.25 "0.9800" "591.1549"  7  6 2010 2011 18420 605 18420 .
    11 "20 MICRONS LTD." "14-06-2010" "42.4500"  40.6 "0.9900" "581.8397" 14  6 2010 2011 18427 605 18427 .
    11 "20 MICRONS LTD." "21-06-2010" "42.0000" 40.35 "0.9900" "578.2570" 21  6 2010 2011 18434 605 18434 .
    11 "20 MICRONS LTD." "28-06-2010" "41.0000"  41.2 "1.0100" "590.4384" 28  6 2010 2011 18441 605 18441 .
    11 "20 MICRONS LTD." "05-07-2010" "41.8500"  41.1 "1.0000" "589.0053"  5  7 2010 2011 18448 606 18448 .
    11 "20 MICRONS LTD." "12-07-2010" "45.1000" 44.85 "1.0300" "642.7466" 12  7 2010 2011 18455 606 18455 .
    11 "20 MICRONS LTD." "19-07-2010" "52.9000"  58.5 "1.1000" "838.3651" 19  7 2010 2011 18462 606 18462 .
    11 "20 MICRONS LTD." "26-07-2010" "53.0000" 51.95 "0.9700" "744.4969" 26  7 2010 2011 18469 606 18469 .
    11 "20 MICRONS LTD." "02-08-2010" "51.5500" 52.35 "1.0300" "750.2293"  2  8 2010 2011 18476 607 18476 .
    11 "20 MICRONS LTD." "09-08-2010" "51.2000" 52.55 "1.0200" "753.0955"  9  8 2010 2011 18483 607 18483 .
    11 "20 MICRONS LTD." "16-08-2010" "51.1500"  51.2 "0.9800" "733.7486" 16  8 2010 2011 18490 607 18490 .
    11 "20 MICRONS LTD." "23-08-2010" "51.1500" 51.75 "0.9800" "741.6307" 23  8 2010 2011 18497 607 18497 .
    11 "20 MICRONS LTD." "30-08-2010" "54.7000" 53.55 "1.0100" "767.4265" 30  8 2010 2011 18504 607 18504 .
    end
    format %td ddate
    format %tm ym
    format %td wdatef

  • #2
    Nihar:
    gaps are the culprits here.
    Code:
    . gen wdatef_01 = date("6oct2008","DMY") in 1
    . replace wdatef_01 = date("07oct2008","DMY") in 2
    . replace wdatef_01 = date("8oct2008","DMY") in 3
    . replace wdatef_01 = date("9oct2008","DMY") in 4
    . format %td wdatef_01
    
    . xtset co_code wdatef_01
    
    Panel variable: co_code (unbalanced)
     Time variable: wdatef_01, 06oct2008 to 09oct2008
             Delta: 1 day
    
    . gen L1_bse_closing_price = L.bse_closing_price if wdatef_01!=.
    
    . list co_code company_name bse_closing_price wdatef_01 L1_bse_closing_price if wdatef_01!=.
    
         +-------------------------------------------------------------+
         | co_code      company_name   bse_cl~e   wdatef_01   L1_bse~e |
         |-------------------------------------------------------------|
      1. |      11   20 MICRONS LTD.       33.3   06oct2008          . |
      2. |      11   20 MICRONS LTD.       24.9   07oct2008       33.3 |
      3. |      11   20 MICRONS LTD.      22.05   08oct2008       24.9 |
      4. |      11   20 MICRONS LTD.       15.1   09oct2008      22.05 |
         +-------------------------------------------------------------+
    
    .
    Kind regards,
    Carlo
    (StataNow 18.5)

    Comment


    • #3
      Carlo:

      I have weekly data. So, I do not understand your point about generating wdatef_01 and replacing it with 6oct2008, 07oct2008, and so on.

      Comment


      • #4
        Your date is in daily form, except that you only have an observation once a week. So if you ask for a lagged value, Stata looks for an observation the previous day, can't find it, and thus returns a missing. So you need to tell Stata not to look one day back, but 7 days back. The easiest way is to specify the delta() option in xtset

        Code:
        xtset co_code wdatef, delta(7)
        gen L1_bse_closing_price = L.bse_closing_price
        ---------------------------------
        Maarten L. Buis
        University of Konstanz
        Department of history and sociology
        box 40
        78457 Konstanz
        Germany
        http://www.maartenbuis.nl
        ---------------------------------

        Comment


        • #5
          Nihar:
          my example aimed at showing how -lag- works.
          Maarten's wise advice did it much better than me.
          Kind regards,
          Carlo
          (StataNow 18.5)

          Comment


          • #6
            Note that the need for delta(7) was mentioned in my answer on your previous thread.

            Comment

            Working...
            X