Announcement

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

  • Adding the Values of Non-Trading Days to Trading Days

    Dear Statlisters,

    I am working on a large dataset. Where day_status = 1 is trading and 0 is non-trading. Observations are unique by permno and date.

    The trading day is when the stock market is open and the non-trading day is when it is closed.

    Required: I need to add the values of uc, ic, be_rf and bu_rf of non-trading days to the subsequent trading days by writing a foreach loop. For example, The values of Saturday and Sunday must be added to the values of Monday IF it is a trading day.

    Please let me know how can I get the required outcome.

    Sample data is here:


    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input double(permno date) byte day_status double uc long ic double(be_rf bu_rf)
    10104 18995 1 2 3 0 3
    10104 18996 1 1 1 0 1
    10104 18997 1 3 5 3 2
    10104 18998 1 1 1 0 1
    10104 18999 0 5 5 0 5
    10104 19000 0 2 2 0 2
    10104 19001 1 2 2 0 2
    10104 19004 1 4 4 1 3
    10104 19005 1 1 2 0 2
    10104 19007 0 1 1 0 1
    10104 19009 1 3 4 0 4
    10104 19010 1 1 1 1 0
    10104 19011 1 5 6 1 5
    10104 19012 1 2 2 1 1
    10104 19013 0 1 1 0 1
    10104 19014 0 1 1 0 1
    10104 19016 1 2 2 0 2
    10104 19017 1 2 2 2 0
    10104 19018 1 3 3 0 3
    10104 19019 1 1 1 0 1
    10104 19022 1 2 2 0 2
    10104 19023 1 2 2 1 1
    10104 19024 1 2 2 0 2
    10104 19025 1 1 1 0 1
    10104 19026 1 2 2 2 0
    end
    format %td date

  • #2
    A loop is perhaps not necessary, if I correctly understand what you want.
    Code:
    gsort permno -date
    replace date = date[_n-1] if day_status==0
    generate day_count = 1
    collapse (sum) day_count uc ic be_rf bu_rf , by(permno date)
    list, clean
    Code:
    . list, clean
    
           permno        date   day_co~t   uc   ic   be_rf   bu_rf  
      1.    10104   03jan2012          1    2    3       0       3  
      2.    10104   04jan2012          1    1    1       0       1  
      3.    10104   05jan2012          1    3    5       3       2  
      4.    10104   06jan2012          1    1    1       0       1  
      5.    10104   09jan2012          3    9    9       0       9  
      6.    10104   12jan2012          1    4    4       1       3  
      7.    10104   13jan2012          1    1    2       0       2  
      8.    10104   17jan2012          2    4    5       0       5  
      9.    10104   18jan2012          1    1    1       1       0  
     10.    10104   19jan2012          1    5    6       1       5  
     11.    10104   20jan2012          1    2    2       1       1  
     12.    10104   24jan2012          3    4    4       0       4  
     13.    10104   25jan2012          1    2    2       2       0  
     14.    10104   26jan2012          1    3    3       0       3  
     15.    10104   27jan2012          1    1    1       0       1  
     16.    10104   30jan2012          1    2    2       0       2  
     17.    10104   31jan2012          1    2    2       1       1  
     18.    10104   01feb2012          1    2    2       0       2  
     19.    10104   02feb2012          1    1    1       0       1  
     20.    10104   03feb2012          1    2    2       2       0

    Comment


    • #3
      Thanks. It solved my problem.

      Comment

      Working...
      X