Announcement

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

  • Count number of observation, three months prior to date

    Hi all,

    Is there any way to count the number of observation three months prior to every date observation?

    I guess I first need to construct the date three months prior to the observed date, and next count the number of observations between those two dates.
    Any suggestions?

    Thanks in advance

    The data looks as follows (don't focus on the id's, those are just to match the info to another dateset) the variable number2 is the total number of observations in my sample.
    date id number2
    1/20/2000 1932 2320
    1/25/2000 1518 2320
    1/26/2000 1452 2320
    1/31/2000 449 2320
    2/2/2000 150 2320
    2/10/2000 2049 2320
    2/14/2000 2050 2320
    2/21/2000 846 2320
    2/22/2000 1978 2320
    3/1/2000 607 2320
    3/2/2000 1807 2320
    3/3/2000 2017 2320
    3/3/2000 755 2320
    3/4/2000 2044 2320
    3/7/2000 2159 2320
    3/7/2000 734 2320
    3/8/2000 1112 2320
    3/9/2000 1420 2320
    3/13/2000 2031 2320
    3/14/2000 1246 2320
    3/15/2000 1255 2320
    3/16/2000 1858 2320
    3/16/2000 1067 2320
    3/16/2000 2162 2320
    3/17/2000 1 2320
    3/20/2000 1275 2320
    3/20/2000 288 2320
    3/20/2000 268 2320
    3/20/2000 2004 2320
    3/22/2000 613 2320
    3/24/2000 1704 2320
    3/24/2000 1305 2320
    3/25/2000 690 2320
    3/27/2000 791 2320
    3/30/2000 259 2320
    3/31/2000 1937 2320
    3/31/2000 260 2320

  • #2
    As advised to you in previous threads, we ask that you provide data examples in a form that can be copied and pasted directly as Stata code.

    Do please read and act on http://www.statalist.org/forums/help#stata before starting your next thread.

    Dates are a particular pain, as they have to be reverse engineered.

    The previous three months could mean various things precisely. Here for the sake of concreteness, I choose one interpretation as the 90 days ending in and including the previous day.

    On rangestat: You need to install that before using it with ssc inst rangestat

    Search the forum for other examples of using rangestat


    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str9 sdate int id
    "1/20/2000" 1932
    "1/25/2000" 1518
    "1/26/2000" 1452
    "1/31/2000"  449
    "2/2/2000"   150
    "2/10/2000" 2049
    "2/14/2000" 2050
    "2/21/2000"  846
    "2/22/2000" 1978
    "3/1/2000"   607
    "3/2/2000"  1807
    "3/3/2000"  2017
    "3/3/2000"   755
    "3/4/2000"  2044
    "3/7/2000"  2159
    "3/7/2000"   734
    "3/8/2000"  1112
    "3/9/2000"  1420
    "3/13/2000" 2031
    "3/14/2000" 1246
    "3/15/2000" 1255
    "3/16/2000" 1858
    "3/16/2000" 1067
    "3/16/2000" 2162
    "3/17/2000"    1
    "3/20/2000" 1275
    "3/20/2000"  288
    "3/20/2000"  268
    "3/20/2000" 2004
    "3/22/2000"  613
    "3/24/2000" 1704
    "3/24/2000" 1305
    "3/25/2000"  690
    "3/27/2000"  791
    "3/30/2000"  259
    "3/31/2000" 1937
    "3/31/2000"  260
    end
    
    gen date = daily(sdate, "MDY")
    
    rangestat (count) prev90=id, interval(date -90 -1)
    
    list 
    
         +-----------------------------------+
         |     sdate     id    date   prev90 |
         |-----------------------------------|
      1. | 1/20/2000   1932   14629        . |
      2. | 1/25/2000   1518   14634        1 |
      3. | 1/26/2000   1452   14635        2 |
      4. | 1/31/2000    449   14640        3 |
      5. |  2/2/2000    150   14642        4 |
         |-----------------------------------|
      6. | 2/10/2000   2049   14650        5 |
      7. | 2/14/2000   2050   14654        6 |
      8. | 2/21/2000    846   14661        7 |
      9. | 2/22/2000   1978   14662        8 |
     10. |  3/1/2000    607   14670        9 |
         |-----------------------------------|
     11. |  3/2/2000   1807   14671       10 |
     12. |  3/3/2000   2017   14672       11 |
     13. |  3/3/2000    755   14672       11 |
     14. |  3/4/2000   2044   14673       13 |
     15. |  3/7/2000   2159   14676       14 |
         |-----------------------------------|
     16. |  3/7/2000    734   14676       14 |
     17. |  3/8/2000   1112   14677       16 |
     18. |  3/9/2000   1420   14678       17 |
     19. | 3/13/2000   2031   14682       18 |
     20. | 3/14/2000   1246   14683       19 |
         |-----------------------------------|
     21. | 3/15/2000   1255   14684       20 |
     22. | 3/16/2000   1858   14685       21 |
     23. | 3/16/2000   1067   14685       21 |
     24. | 3/16/2000   2162   14685       21 |
     25. | 3/17/2000      1   14686       24 |
         |-----------------------------------|
     26. | 3/20/2000   1275   14689       25 |
     27. | 3/20/2000    288   14689       25 |
     28. | 3/20/2000    268   14689       25 |
     29. | 3/20/2000   2004   14689       25 |
     30. | 3/22/2000    613   14691       29 |
         |-----------------------------------|
     31. | 3/24/2000   1704   14693       30 |
     32. | 3/24/2000   1305   14693       30 |
     33. | 3/25/2000    690   14694       32 |
     34. | 3/27/2000    791   14696       33 |
     35. | 3/30/2000    259   14699       34 |
         |-----------------------------------|
     36. | 3/31/2000   1937   14700       35 |
     37. | 3/31/2000    260   14700       35 |
         +-----------------------------------+

    Comment


    • #3
      Thanks Nick, your example seems to be doing exactly what I want. That is a brilliant program you guys made. I'll try to use it for some other variables too because this will probably also allow me to make an interval to calculate return volatility.


      Comment

      Working...
      X