Announcement

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

  • No observations while using forvalues

    Hello Statalist Forum,

    I've looked a lot on this forum to find a solution to my problem, but couldn't find it. I've tried to follow princeton event study manual, but sadly didn't work out as planned.
    https://dss.princeton.edu/online_hel...ventstudy.html

    Main idea:
    I'm constructing an event study on abnormal returns around a publication date of a certain index. My data consist of aprox 263 companies with daily data for the last 9 years ( 2010-2018). In my data i've variables consisting of the estimation/event window ( t10 till t-160 ) and a year variable ( 2010 - 2018 ) so i can seperate the different event windows.
    I'm trying to predict the normal return using the code stated below, but it keeps saying no observations. If i exclude the "year==2011", the normal return will be computed just fine but for the whole period, but i only want to predict it via the estimation window of that year.

    Hopefully you guys can help me with this problem.
    Many thanks

    Regards Mathijs



    Code:
    sum id_firm
    local N `r(max)'
    
    gen NR_MMM=.
    
    forvalues i = 1/`N'{
    quietly reg r_firm r_mkt if (id_firm==`i' & t_event<-1 & t_event>-160 & year==2011)
    quietly predict r if id_firm==`i'
    quietly replace NR_MMM = r if (id_firm==`i' & year==2011)
    quietly drop r
    }

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input int(Date id_firm) double(r_firm r_mkt) int(t_event year)
    19025 230  -.0025229357798165005   .0009238920203358916 -160 2012
    19753 190     .04309623430962348    .008499461024778178 -160 2014
    20481  55    .017344726437954334   -.013286579185272403 -160 2016
    21216  29  -.0016835016835016834  -.0057290387665841274 -160 2018
    20845   1    .004322766570605188 -.00041036062882888227 -160 2017
    21216 101   -.029866781263429322   -.014070304515416828 -160 2018
    19389 117  -.0008529629693450356   -.007311275622280882 -160 2013
    20845 162  -.0005654509471304248 -.00041132637191249277 -160 2017
    18654 253   .0031395348837210493   .0025931880654076122 -160 2011
    19753 104   .0019767759762650966    .005542485169522096 -160 2014
    20481  77   -.027538940809968857     -.0244139656425276 -160 2016
    20481  29  -.0029585798816568047   -.009780698020322642 -160 2016
    19025  64    .007498183259562601   .0009238920203358916 -160 2012
    18654 240   -.008320852055250457    .003960798894399681 -160 2011
    20845  96   .0012170821511255409 -.00041036062882888227 -160 2017
    20845  86  -.0006589062156819929  -.0038222696951609016 -160 2017
    19025 147  -.0018137847642080494   .0027320294821615754 -160 2012
    19025 164   .0019557728636515385   -.000906108986788931 -160 2012
    18290 195    -.02115320045175681   -.018229018996314128 -160 2010
    20481  61   -.021164021164021184   -.034915054782913554 -160 2016
    19025 191   -.003249573212734672   .0009238920203358916 -160 2012
    20845 126    .003335804299481013 -.00041036062882888227 -160 2017
    19025 228                      .   -.000906108986788931 -160 2012
    19025  82    .006389776357827529   .0027320294821615754 -160 2012
    20481 204   -.010085917071348524   -.009780698020322642 -160 2016
    19025  34  -.0011225204294690466    .008704551203918855 -160 2012
    20481 249    -.02539323234082064   -.013286579185272403 -160 2016
    18654 212    .006600660066006601  -.0006918838506268181 -160 2011
    18654   2    .011499336576736028   -.004645843539880874 -160 2011
    19389 261   .0030651340996169017   .0004006551252724098 -160 2013
    18290 102   -.011016949152542439   -.013671369723985602 -160 2010
    21216 165    -.07318536292741461    -.03084133269141974 -160 2018
    20845  92    .006963788300835654  -.0021383160402405842 -160 2017
    18290 249   -.027441320246588963    -.01888664290653764 -160 2010
    19389   5    -.04989571634846782   -.024451988473967307 -160 2013
    19753 161   -.004789272030651408  -.0052465955687669465 -160 2014
    20845 231                      . -.00041036062882888227 -160 2017
    21216  80   -.000961307378034106   -.003480714957667023 -160 2018
    18654 109    -.01069804760631181   .0027684343119748623 -160 2011
    18290 194    .009378746209717315   -.013748042633690364 -160 2010
    21216  30     -.0198230088495574  -.0057290387665841274 -160 2018
    20481 218   -.034782608695652195   -.013286579185272403 -160 2016
    20845 181                      . -.00041132637191249277 -160 2017
    18290  80   -.006535947712418266    -.01412053152525962 -160 2010
    20845  32                      .    .002033351334687284 -160 2017
    20845 200    .005118534482758559  -.0021383160402405842 -160 2017
    19389 139   -.004910852541697981   .0004006551252724098 -160 2013
    19389  42   .0037240229431762203   -.007311275622280882 -160 2013
    19753 149 -.00013314545030901345   .0039360675525585516 -160 2014
    20481 152                      .     -.0244139656425276 -160 2016
    20845 206   .0019074868860276585    .002137692174711363 -160 2017
    20481 139   -.009038238702201636   -.020355222115953017 -160 2016
    20481  22   -.028169014084507043   -.009780698020322642 -160 2016
    19389  84     .02689486552567241  -.0035879254802366123 -160 2013
    18654 114  -.0001436781609195729  -.0006918838506268181 -160 2011
    19753  79   -.043214556482183364   .0039360675525585516 -160 2014
    18654 235    .016705069124423995    .009586230130776991 -160 2011
    19753  61    -.03406326034063263    .003833500022236543 -160 2014
    19753  67    .006464445549477999    .005542485169522096 -160 2014
    20845 125   -.021739130434782723   -.007281091070833001 -160 2017
    18654 201    .011919241060569253    .003960798894399681 -160 2011
    20481  52                      .   -.020355222115953017 -160 2016
    18290 144    -.01512287334593574   -.017931964365187236 -160 2010
    18290  21   -.009112939869743007    -.01412053152525962 -160 2010
    21216  88   -.008610567514677198   .0014633901474704967 -160 2018
    19389 166  -.0005540166204985835    -.00983273264947775 -160 2013
    18290 228    .001288659793814433   -.004730396738467131 -160 2010
    19025 122   -.010162656271004095   .0009238920203358916 -160 2012
    18290 148   -.012016673450197206    -.01888664290653764 -160 2010
    18654 193                      .  -.0006918838506268181 -160 2011
    20845  57   -.011997658993367254    .003606625416629603 -160 2017
    18654 261    .005048543689320432   -.004645843539880874 -160 2011
    18290 180   -.001011122345803949    -.01412053152525962 -160 2010
    21216  85    .007442849548112737   .0054546025170260515 -160 2018
    20481  30    .011702986279257489   -.009780698020322642 -160 2016
    18654 189    .003181818181818182  -.0006918838506268181 -160 2011
    20481 240   -.019933774834437097     -.0244139656425276 -160 2016
    19025  62     .00890906555522843   .0009238920203358916 -160 2012
    18290 165   -.001082251082251059   .0038328423100025028 -160 2010
    20845 242   -.008926717874195552    .003606625416629603 -160 2017
    21216  78   -.004935834155972359  -.0057290387665841274 -160 2018
    18654 202     .02214904157792387   .0027684343119748623 -160 2011
    19025  32    .024425495013730182    .006152778451469831 -160 2012
    21216 254   -.006703910614525076   -.014070304515416828 -160 2018
    21216 249   -.019212598425196833   -.004994591320939908 -160 2018
    19025 247   -.023809523809523808   .0009238920203358916 -160 2012
    18654  56                      .   -.006934446479533244 -160 2011
    19025 112   .0034812880765882634   .0007791305653860964 -160 2012
    21216  69   -.014165390505359877  -.0057290387665841274 -160 2018
    19025 240  -.0026423037305746363    .005892718963099062 -160 2012
    20845 215     .00723589001447178  -.0021383160402405842 -160 2017
    19389 184   -.005788712011577424   -.007311275622280882 -160 2013
    20845 131     .00493177708367577  -.0021383160402405842 -160 2017
    20845  78     .03596450256889304 -.00041036062882888227 -160 2017
    20845 124    .017813971243160778  -.0021383160402405842 -160 2017
    20845 127                      . -.00041036062882888227 -160 2017
    18290  91   .0052238805970148405    .002316134549601034 -160 2010
    18654 236   .0015661707126076406    .013837415367837527 -160 2011
    18290  67   -.017308524448290723    -.01888664290653764 -160 2010
    18290  54    -.02000871742498713    -.01888664290653764 -160 2010
    end
    format %tdnn/dd/CCYY Date


  • #2
    Mathis:
    welcome to this forum.
    Your data excerpt does not allow to replicate your problem.
    Hence, with a bit of surgery, I tried my best to sketch a hopefully useful toy-example:
    Code:
    g new_id=1 in 1/50
    replace new_id=2 if new_id==.
    . forvalues i = 1(1)2{
      2. quietly reg r_firm r_mkt if ( new_id ==`i' &  t_event<=-160 & year==2011)
      3. quietly predict r if new_id ==`i'
      4. quietly replace NR_MMM = r if new_id ==`i' & year==2011
      5. quietly drop r
      6. }
    . list if year==2011
    
         +-------------------------------------------------------------------------------------+
         |      Date   id_firm       r_firm        r_mkt   t_event   year      NR_MMM   new_id |
         |-------------------------------------------------------------------------------------|
     15. | 1/27/2011       193            .   -.00069188      -160   2011    .0064894        2 |
     16. | 1/27/2011       201    .01191924     .0039608      -160   2011    .0069413        2 |
     17. | 1/27/2011       253    .00313953    .00259319      -160   2011    .0068085        2 |
     18. | 1/27/2011       109   -.01069805    .00276843      -160   2011    -.010629        1 |
     19. | 1/27/2011       236    .00156617    .01383742      -160   2011    .0079007        2 |
         |-------------------------------------------------------------------------------------|
     20. | 1/27/2011       212    .00660066   -.00069188      -160   2011    .0064894        2 |
     21. | 1/27/2011       202    .02214904    .00276843      -160   2011    .0068255        2 |
     22. | 1/27/2011       261    .00504854   -.00464584      -160   2011    .0061053        2 |
     23. | 1/27/2011       235    .01670507    .00958623      -160   2011    .0074878        2 |
     24. | 1/27/2011         2    .01149934   -.00464584      -160   2011    .0115598        1 |
         |-------------------------------------------------------------------------------------|
     25. | 1/27/2011       189    .00318182   -.00069188      -160   2011    .0064894        2 |
     26. | 1/27/2011       114   -.00014368   -.00069188      -160   2011   -.0002732        1 |
     27. | 1/27/2011       240   -.00832085     .0039608      -160   2011    .0069413        2 |
     28. | 1/27/2011        56            .   -.00693445      -160   2011    .0184089        1 |
         +-------------------------------------------------------------------------------------+
    As an aside, if you have multiple observations for the same -id-, why not considering -xtreg- instead of -regress-?
    Kind regards,
    Carlo
    (Stata 18.0 SE)

    Comment


    • #3
      Hello Carlo,

      Thank you for the quick response! I've tried to to mimic your example, but sadly it still gives me the same error. I also switched to xtreg, but still the same error ( it now gives the error insufficient observations )

      I've included my dataset in the wetransfer link below, so hopefully that will help replicate the problem.
      https://we.tl/t-TkIp9GltTV

      Leaving out the section "year==2011" will let me run the code just fine, but i'm not sure what i'm doing wrong there?

      Thanks in advance!



      Comment


      • #4
        You are asking Stata to loop over firms with identifier from 1 to the maximum seen. If there are gaps in the sequence (e.g. there is no firm 42), then Stata will complain. If there are missing values for a firm on either variable entered into the regression, then Stata will complain that it can't do the regression.

        But you don't need a loop at all. Commands exist to do this for you that deal cleanly with gaps and missings. rangestat (SSC) is one such. Here is a guess at code for your problem.

        Code:
        rangestat (reg) r_firm r_mkt if inrange(t_event, -159, -2) & year==2011, int(id_firm 0 0)  
        gen NR_MMM = b_cons + b_r_mkt * r_mkt if year == 2011

        Comment


        • #5
          Hello Nick,


          Your code seems to work if i want to regress 2011 on its own, but if i want to regress the years 2011 till 2018, it states that reg_nobs is already defined.

          Is there an easy way of fixing this?

          Code:
          rangestat (reg) r_firm r_mkt if inrange(t_event, -160, -2) & year==2011, int(id_firm 0 0)  
          gen NR_MMM = b_cons + b_r_mkt * r_mkt if year == 2011
          rangestat (reg) r_firm r_mkt if inrange(t_event, -160, -2) & year==2012, int(id_firm 0 0)  
          gen NR_MMM = b_cons + b_r_mkt * r_mkt if year == 2012
          rangestat (reg) r_firm r_mkt if inrange(t_event, -160, -2) & year==2013, int(id_firm 0 0)  
          gen NR_MMM = b_cons + b_r_mkt * r_mkt if year == 2013
          rangestat (reg) r_firm r_mkt if inrange(t_event, -160, -2) & year==2014, int(id_firm 0 0)  
          gen NR_MMM = b_cons + b_r_mkt * r_mkt if year == 2014
          rangestat (reg) r_firm r_mkt if inrange(t_event, -160, -2) & year==2015, int(id_firm 0 0)  
          gen NR_MMM = b_cons + b_r_mkt * r_mkt if year == 2015
          rangestat (reg) r_firm r_mkt if inrange(t_event, -160, -2) & year==2016, int(id_firm 0 0)  
          gen NR_MMM = b_cons + b_r_mkt * r_mkt if year == 2016
          rangestat (reg) r_firm r_mkt if inrange(t_event, -160, -2) & year==2017, int(id_firm 0 0)  
          gen NR_MMM = b_cons + b_r_mkt * r_mkt if year == 2017
          rangestat (reg) r_firm r_mkt if inrange(t_event, -160, -2) & year==2018, int(id_firm 0 0)  
          gen NR_MMM = b_cons + b_r_mkt * r_mkt if year == 2018
          
          // Generate abnormal returns
          gen AR_MMM = r_firm - NR_MMM
          preserve
          Or
          Code:
          // Generating Market Model Returns per year
          
          rangestat (reg) r_firm r_mkt if inrange(t_event, -160, -2) & year==2011, int(id_firm 0 0)  
          rangestat (reg) r_firm r_mkt if inrange(t_event, -160, -2) & year==2012, int(id_firm 0 0)  
          rangestat (reg) r_firm r_mkt if inrange(t_event, -160, -2) & year==2013, int(id_firm 0 0)  
          rangestat (reg) r_firm r_mkt if inrange(t_event, -160, -2) & year==2014, int(id_firm 0 0)  
          rangestat (reg) r_firm r_mkt if inrange(t_event, -160, -2) & year==2015, int(id_firm 0 0)  
          rangestat (reg) r_firm r_mkt if inrange(t_event, -160, -2) & year==2016, int(id_firm 0 0)  
          rangestat (reg) r_firm r_mkt if inrange(t_event, -160, -2) & year==2017, int(id_firm 0 0)  
          rangestat (reg) r_firm r_mkt if inrange(t_event, -160, -2) & year==2018, int(id_firm 0 0)  
          gen NR_MMM = b_cons + b_r_mkt * r_mkt
          
          // Generate abnormal returns
          gen AR_MMM = r_firm - NR_MMM
          preserve
          Last edited by Mathis Remmers; 14 Jun 2019, 06:33. Reason: Small grammar mistakes

          Comment


          • #6
            Nothing peculiar there to rangestat. It's always true that trying to generate a variable that already exists is an error. One generate statement creates a variable; thereafter you need to replace such a variable.

            But it is easier than that. In what I read, I saw interest only in 2011. As you want to get results for several years, you can do that all at once.

            Code:
            rangestat (reg) r_firm r_mkt if inrange(t_event, -159, -2), int(id_firm 0 0) by(year)  
            gen NR_MMM = b_cons + b_r_mkt * r_mkt
            Last edited by Nick Cox; 14 Jun 2019, 06:53.

            Comment


            • #7
              Many thanks!

              Comment

              Working...
              X