Announcement

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

  • Generate lag for variable

    Hi Statalist,

    I have a simple question, but can not solve it yet. I have tried many times, but yet no solution.
    In Panel data, I would like to generate lag for dailyMR and lag for liquidity. I try use the code below, but the result appear was "not sorted" r5.
    Does anyone have an idea what is wrong with my data. From data below, it seems all sorted according to company and caldate.

    Code:
    gen lag_liquidty = L.liquidity
    not sorted
    r(5);
    Code:
    gen lag_dailyMR = L.dailyMR
    not sorted
    r(5);
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float idc str21 company float(caldate dailyMR return turnover liquidity)
    1 "ALUAR" 16803             .             .              .              .
    1 "ALUAR" 16804     .57453316    -1.0000079   .00026832195   -.0008290283
    1 "ALUAR" 16805      6.143557     1.4962867    .0001801904   -.0011104317
    1 "ALUAR" 16806     -.2811049      .4938291  -.00005071409   -.0005513455
    1 "ALUAR" 16807      1.493126      1.951283  -.00014974715    -.003205513
    1 "ALUAR" 16810     1.2772804      .4819319  .000061128725    -.000435019
    1 "ALUAR" 16811 2.2725384e-07  3.667978e-06   -.0002303494 -1.0946313e-08
    1 "ALUAR" 16812      1.130413  3.667978e-06  -.00015540613   -8.14151e-09
    1 "ALUAR" 16813     -.9894335  3.667978e-06  -.00013939025 -8.3142435e-09
    1 "ALUAR" 16814    -1.4835738     -.9661874  -.00020480275   -.0044442695
    1 "ALUAR" 16817       2.43612     -.9756147   -.0001404652   -.0029261224
    1 "ALUAR" 16818      4.256966     1.9418105   .00008240703   -.0021166024
    1 "ALUAR" 16819     1.8582594     2.3754122   6.999413e-06   -.0031803616
    1 "ALUAR" 16820     1.3807563 -5.372813e-06   .00008234708  -5.601704e-09
    1 "ALUAR" 16821     -.4033334     -2.375414   -.0001295466    -.006384655
    1 "ALUAR" 16824    -.29552194  3.667978e-06    -.000137045 -1.1232173e-08
    1 "ALUAR" 16825      .3269679     -.9661874  -.00009242458   -.0022897667
    1 "ALUAR" 16826   -.011237455     -.9756147  -.00014652606    -.003788391
    1 "ALUAR" 16827      .8484781     1.4598818  -.00018611016    -.011132359
    1 "ALUAR" 16828      2.494687      1.438877 -.000028606446   -.0026522144
    end
    format %td caldate
    Thank you in advanced.

  • #2
    You must tsset or xtset your data before you can use lag operators.

    Code:
    egen id = group(company), label 
    xtset id caldate 
    gen lag_liquidity = L.liquidity

    Comment


    • #3
      It worked. Thank you very much Nick.

      Comment


      • #4
        Nick Cox hi nick! I hope you are doing well. I am having a similar issue. I ma writing my thesis in which I need to create a lagged dependent variable. and my lecturer has told me this:
        get rid of "lagroa" I am afraid you have made an error in its creation. Instead, use "l.roa"

        I tried doing that using these commands:
        gen l.roa=l1.roa
        but i am getting an error. can you please guide me?

        Comment


        • #5
          Periods aren't allowed in variable names.

          Comment


          • #6
            Originally posted by Nick Cox View Post
            You must tsset or xtset your data before you can use lag operators.

            Code:
            egen id = group(company), label
            xtset id caldate
            gen lag_liquidity = L.liquidity
            Dear Nick Cox,

            If I want to find a lagged value for a string variable (auditor_name) and the result is also a string variable, how should I do?
            For instance, the lagged value auditor_name for gvkey 1004 in year 2003 will be a string "KPMG LLP"
            I try with the normal way we do for numeric variables, but the results are all missing values (.)
            Thank you for your consideration.

            For examples:
            Code:
            * Example generated by -dataex-. To install: ssc install dataex
            clear
            input long gvkey double year str70 auditor_name
            1004 2002 "KPMG LLP"                         
            1004 2003 "KPMG LLP"                         
            1004 2004 "KPMG LLP"                         
            1004 2005 "KPMG LLP"                         
            1004 2006 "KPMG LLP"                         
            1004 2007 "KPMG LLP"                         
            1004 2008 "KPMG LLP"                         
            1004 2009 "KPMG LLP"                         
            1004 2010 "KPMG LLP"                         
            1004 2011 "KPMG LLP"                         
            1004 2012 "KPMG LLP"                         
            1004 2013 "KPMG LLP"                         
            1004 2014 "KPMG LLP"                         
            1004 2015 "KPMG LLP"                         
            1004 2016 "KPMG LLP"                         
            1004 2017 "KPMG LLP"                         
            1013 2002 "Ernst & Young LLP"                
            1013 2003 "Ernst & Young LLP"                
            1013 2004 "Ernst & Young LLP"                
            1013 2005 "Ernst & Young LLP"                
            1013 2006 "Ernst & Young LLP"                
            1013 2007 "Ernst & Young LLP"                
            1013 2008 "Ernst & Young LLP"                
            1013 2009 "Ernst & Young LLP"                
            1013 2010 "Ernst & Young LLP"                
            1021 2002 "Ernst & Young LLP"                
            1021 2003 "Ernst & Young LLP"                
            1021 2004 "Goldstein Golub Kessler LLP (GGK)"
            1021 2005 "Goldstein Golub Kessler LLP (GGK)"
            1021 2006 "Goldstein Golub Kessler LLP (GGK)"
            1021 2007 "Goldstein Golub Kessler LLP (GGK)"
            1021 2008 "RSM US LLP"                       
            1034 2002 "PricewaterhouseCoopers LLP"       
            1034 2003 "PricewaterhouseCoopers LLP"       
            1034 2004 "PricewaterhouseCoopers LLP"       
            1034 2005 "BDO USA LLP"                      
            1034 2006 "BDO USA LLP"                      
            1034 2007 "BDO USA LLP"                      
            1050 2002 "Deloitte & Touche LLP"            
            1050 2003 "Deloitte & Touche LLP"            
            1050 2004 "Deloitte & Touche LLP"            
            1050 2005 "Battelle Rippe Kingston LLP"      
            1050 2006 "Battelle Rippe Kingston LLP"      
            1050 2007 "Battelle Rippe Kingston LLP"      
            1050 2008 "BDO USA LLP"                      
            1050 2009 "BDO USA LLP"                      
            1050 2010 "BDO USA LLP"                      
            1050 2011 "BDO USA LLP"                      
            1050 2012 "BDO USA LLP"                      
            1050 2013 "BDO USA LLP"                      
            1050 2014 "BDO USA LLP"                      
            1050 2015 "BDO USA LLP"                      
            1050 2016 "BDO USA LLP"                      
            1050 2017 "BDO USA LLP"                      
            1050 2018 "BDO USA LLP"                      
            1056 2002 "KPMG LLP"                         
            1056 2003 "KPMG LLP"                         
            1056 2004 "KPMG LLP"                         
            1056 2005 "KPMG LLP"                         
            1056 2006 "KPMG LLP"                         
            1072 2002 "PricewaterhouseCoopers LLP"       
            1072 2003 "PricewaterhouseCoopers LLP"       
            1072 2004 "PricewaterhouseCoopers LLP"       
            1072 2005 "PricewaterhouseCoopers LLP"       
            1072 2006 "PricewaterhouseCoopers LLP"       
            1072 2007 "PricewaterhouseCoopers LLP"       
            1072 2008 "PricewaterhouseCoopers LLP"       
            1072 2009 "PricewaterhouseCoopers LLP"       
            1072 2010 "PricewaterhouseCoopers LLP"       
            1072 2011 "PricewaterhouseCoopers LLP"       
            1072 2012 "PricewaterhouseCoopers LLP"       
            1072 2013 "PricewaterhouseCoopers LLP"       
            1072 2014 "PricewaterhouseCoopers LLP"       
            1072 2015 "PricewaterhouseCoopers LLP"       
            1072 2016 "PricewaterhouseCoopers LLP"       
            1072 2017 "PricewaterhouseCoopers LLP"       
            1072 2018 "PricewaterhouseCoopers LLP"       
            1076 2002 "Ernst & Young LLP"                
            1076 2003 "Ernst & Young LLP"                
            1076 2004 "Ernst & Young LLP"                
            1076 2005 "Ernst & Young LLP"                
            1076 2006 "Ernst & Young LLP"                
            1076 2007 "Ernst & Young LLP"                
            1076 2008 "Ernst & Young LLP"                
            1076 2009 "Ernst & Young LLP"                
            1076 2010 "Ernst & Young LLP"                
            1076 2011 "Ernst & Young LLP"                
            1076 2012 "Ernst & Young LLP"                
            1076 2013 "Ernst & Young LLP"                
            1076 2014 "Ernst & Young LLP"                
            1076 2015 "Ernst & Young LLP"                
            1076 2016 "Ernst & Young LLP"                
            1076 2017 "Ernst & Young LLP"                
            1076 2018 "Ernst & Young LLP"                
            1078 2002 "Deloitte & Touche LLP"            
            1078 2003 "Deloitte & Touche LLP"            
            1078 2004 "Deloitte & Touche LLP"            
            1078 2005 "Deloitte & Touche LLP"            
            1078 2006 "Deloitte & Touche LLP"            
            1078 2007 "Deloitte & Touche LLP"            
            end

            Comment


            • #7
              Can you please guide me on this:

              sort fyearq fqtr GVKEY
              gen Lag_Loss = L.LOSS
              not sorted
              r(5);

              Comment


              • #8
                Code:
                h tsset

                Comment

                Working...
                X