Announcement

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

  • Count Number of observations within a certain range

    Dear Stata Community,

    I am very sorry if this question is rather basic, but I am desperately seeking help for my master's thesis. Attached is a sample of my dataset.

    What I need to find out is: per fund id : how many observations are there between the actual monthnum[_n] and monthnum[_n]-36. In other words, as my panel is not balanced, I need to define the time window for a rolling 36-month regression (the rolling command is way too slow, as my dataset contains 1m+ observations).

    I tried the following code:



    foreach i in id {

    forval j = 204/395 {

    egen temp = count(monthnum) if monthnum >= (`j'-36) & monthnum <=`j' & id == `i'

    replace count_id = temp if monthnum == `j'

    drop temp

    }

    }


    but somehow it does not properly count the number of observations, and simply adds 74 for all values....

    can you please help me with this issue?

    Thank you so much for helping an unexperienced stataist!



    Attached Files

  • #2
    Take a look at rangestat (from SSC) and search this forum for examples of using it for rolling regressions. Here's a recent post that you may find useful.

    Comment


    • #3
      worked perfectly fine! Thank you so much, Robert!

      Comment

      Working...
      X