Announcement

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

  • Count different portfolios

    Hi,

    My dataset is as following:


    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input long gvkey float(time change5 count)
    101340 422 2 20
    101538 422 1 21
    208224 422 1 21
    100131 422 1 21
    100499 422 4 14
    101317 422 4 14
    102283 422 1 21
    221020 422 2 20
    102902 422 5 16
     19591 422 2 20
     12384 422 4 14
     19565 422 5 16
    100760 422 4 14
    100862 422 1 21
    100095 422 1 21
    101048 422 1 21
     11749 422 2 20
    100149 422 3 19
    100980 422 1 21
     18636 422 3 19
    100619 422 3 19
    100913 422 4 14
    100045 422 2 20
    102569 423 2 20
    100816 423 4 14
    101310 423 1 21
     19904 423 5 16
    101207 423 4 14
    100728 423 5 16
    101343 423 3 19
    222305 423 5 16
    100774 423 4 14
    200189 423 3 19
     14620 423 3 19
    220392 423 2 20
     19565 423 5 16
    100951 423 3 19
     19579 423 5 16
      4439 423 1 21
     12368 423 5 16
    100060 423 1 21
    end
    format %tm time
    Change5 are portfolios which are formed each month. I would like to count how many portfolios consist of less than 30 stocks (count).In my head I have something like the following formula: count if time, change5 and count is the same. So for example:
    Code:
    input long gvkey float(time change5 count)
    101538 422 1 21
    208224 422 1 21
    100131 422 1 21
    102283 422 1 21
    100862 422 1 21
    100095 422 1 21
    101048 422 1 21
    100980 422 1 21
    All these observations from the sample above are in the same portfolio, so this would count as 1 portfolio.

    Is there an easy formula for this?

    Thanks

  • #2
    Juan:
    are you looking for something along the following lines?
    Code:
    . bysort gvkey (time) change5: gen wanted=1 if count<30
    
    . total wanted, over( change5)
    
    Total estimation                                Number of obs = 41
    
    ------------------------------------------------------------------
                     |      Total   Std. err.     [95% conf. interval]
    -----------------+------------------------------------------------
    c.wanted@change5 |
                  1  |         11          0             .           .
                  2  |          7          0             .           .
                  3  |          7          0             .           .
                  4  |          8          0             .           .
                  5  |          8          0             .           .
    ------------------------------------------------------------------
    Kind regards,
    Carlo
    (Stata 19.0)

    Comment


    • #3
      Hi Carlo,

      Thanks for your response, but with the code you provided I get the number of observations per change5, but what I want is the number of portfolios. So if the time and change5 are the same, it is a portfolio. So I want to count how many different portfolios are there, counting if time and change5 are the same. In the example above (#1), you can see that the time (422) and the change5 (1) are the same, so all those observations are in 1 portfolio.

      Comment


      • #4
        Juan:
        see -collapse-.
        Kind regards,
        Carlo
        (Stata 19.0)

        Comment


        • #5
          Thanks Carlo, it worked!

          Comment

          Working...
          X