Announcement

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

  • #16
    The capture command should do that. Here is an example where I add observations for Apr 1996. Removing the cap commands (highlighted in red), you will get an error as there are not enough observations for Mar. 2016. However, leaving it in will allow the code to skip March and proceed to April.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input long Date float(strike_price_01 impl_volatility_01 date_01)
    19960104         0       . 19960132
    19960110  310.4686 .369596 19960228
    19960110         0       . 19960332
    19960115  426.3265 .260744 19960430
    19960115  59.92363 .190384        .
    19960205  310.4686  .37235        .
    19960205 272.24646 .170089        .
    19960205  53.83344 .102904        .
    19960213         0       .        .
    19960213         0       .        .
    19960213  33.57839  .18374        .
    19960301 363.28085 .128773        .
    19960310         0       .        .
    19960325         0       .        .
    19960225  424.2615 .211032        .
    19960421  70.57839  .38374        .
    19960424         0       .        .
    19960428 175.28085 .328773        .
    end
    drop if missing(Date)
    gen year= int(Date/10000)
    gen month=  int(Date/100) - (year*100)
    gen day= Date-int(Date/100)*100
    gen date= mdy(month, day, year), after(Date)
    format date %td
    bys year month (day): gen lastdayofmonth=_n==_N
    bys year month day (lastdayofmonth): replace lastdayofmonth= lastdayofmonth[_N]
    gen yhat=.
    gen yearmonth= ym(year, month)
    format yearmonth %tm
    levelsof yearmonth, local(months)
    foreach m of local months{
        cap mkspline strike_p_`m' = strike_price  if yearmonth==`m', cubic  nknots(3)
        quietly cap regress impl_volatility strike_p_`m'*  if yearmonth==`m', noconstant            
        cap predict y_hat_`m'      
        cap replace yhat=  y_hat_`m' if yearmonth==`m'  
        cap drop strike_p_`m'*  y_hat_`m'    
    }
    l date strike_price_01 impl_volatility_01 yearmonth yhat, sepby(yearmonth)
    Res.:

    *WITHOUT capture
    Code:
    (option xb assumed; fitted values)
    (13 missing values generated)
    (5 real changes made)
    (option xb assumed; fitted values)
    (11 missing values generated)
    (7 real changes made)
    Sample size too small for this many knots.
    invalid syntax
    r(198);

    *WITH capture
    Code:
    . l date strike_price_01 impl_volatility_01 yearmonth yhat, sepby(yearmonth)
    
         +-------------------------------------------------------+
         |      date   strik~01   impl_~01   yearmo~h       yhat |
         |-------------------------------------------------------|
      1. | 04jan1996          0          .     1996m1          0 |
      2. | 10jan1996   310.4686    .369596     1996m1    .359827 |
      3. | 10jan1996          0          .     1996m1          0 |
      4. | 15jan1996   59.92363    .190384     1996m1   .2003273 |
      5. | 15jan1996   426.3265    .260744     1996m1   .2664606 |
         |-------------------------------------------------------|
      6. | 05feb1996   310.4686     .37235     1996m2    .263638 |
      7. | 05feb1996   272.2465    .170089     1996m2    .275721 |
      8. | 05feb1996   53.83344    .102904     1996m2   .1363338 |
      9. | 13feb1996          0          .     1996m2          0 |
     10. | 13feb1996          0          .     1996m2          0 |
     11. | 13feb1996   33.57839     .18374     1996m2   .0884886 |
     12. | 25feb1996   424.2615    .211032     1996m2   .2260993 |
         |-------------------------------------------------------|
     13. | 01mar1996   363.2809    .128773     1996m3          . |
     14. | 10mar1996          0          .     1996m3          . |
     15. | 25mar1996          0          .     1996m3          . |
         |-------------------------------------------------------|
     16. | 21apr1996   70.57839     .38374     1996m4     .38374 |
     17. | 24apr1996          0          .     1996m4          0 |
     18. | 28apr1996   175.2809    .328773     1996m4    .328773 |
         +-------------------------------------------------------+
    
    .
    Last edited by Andrew Musau; 16 May 2021, 16:27.

    Comment


    • #17
      Thank you a lot for your replies and for all the help!

      Comment

      Working...
      X