Announcement

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

  • Count distinct observations per double group

    Hi everybody,
    I have the following dataset:
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input long permno float date long numboftrades float(MeR RF Exret) double firstbeta float idiovola byte idiodec
    61882 14609 .  .0052  .0002  .001946  .10922990434478642 .011844507 1
    25590 10521 .  .0017 .00029 -.020838   .6093023585900306 .012746225 1
    50068  5638 . -.0124 .00019  -.00019  .06829672850382361 .013093803 1
    24424  8623 .   .001 .00033 -.005855  .29846679803207005 .011187988 1
    52679 14853 .  .0119 .00022  .003228   .0070179771949696 .011636469 1
    24053 10798 .  .0002 .00035  -.00035   .2501415167644749  .01242943 1
    18964  2400 . -.0036 .00018 -.019535   .6457935585326624   .0120517 1
    23536 13173 .  -.004 .00019 -.012238  .35999242316746016 .011240058 1
    50876 10108 . -.0091 .00021 -.019895  1.2651958571414073  .01437045 1
    29647  7560 .  .0008 .00036 -.007936 .038934937312526834  .01219917 1
    92901 18554 . -.0167 .00001  .004166 .048919444992476656 .006706394 1
    75165 11591 . -.0003 .00023 -.016897  .36884666705525304 .013412146 1
    77147 14927 . -.0139 .00024  .005442 .039521170973657024 .010339692 1
    75105 12533 .    .01 .00014  .008062   .7817216117491121 .010505214 1
    20853  2909 .  -.001 .00017  .002592   .2410123276293364 .011108984 1
    18964  6464 . -.0031 .00021  -.00021   .6943091508827608   .0120517 1
    18964  5219 .  .0156 .00036  -.00036   .6933488321491824   .0120517 1
    19916  2587 . -.0008  .0002 -.014745  .39923746584354414 .013699743 1
    77116 14899 .  .0024 .00025 -.004299  .08216729793739826 .009576986 1
    91794 18168 .  .0179      0  .024186  1.0507487349821079  .01300161 1
    end
    format %td date
    I have time series of different lengths for different permno values. Idiodec is a variable which gives the number of decile to which an observation belongs to (e.g. 1st decile). Now, I wish to create a variable that gives me the number of distinct permno values for every value of idiodec. If I try the command

    by permno idiodec, sort: egen newvar=count(permno)

    I don't get what I want. Using

    by idiodec, sort: distinct permno

    I get the what I want but I can only see it,whereas I would like to put it into a variable. Some knows the solution?
    Thank you

  • #2
    Behind distinct (from the Stata Journal, as you are asked to explain: FAQ Advice #12) lies a discussion of this territory at https://www.stata-journal.com/sjpdf....iclenum=dm0042 and the price of this advice is an obligation to cite that paper.

    Code:
    egen tag = tag(permno idiodec)
    egen ndistinct = total(tag), by(idiodec)
    is a device from that paper (and one requiring no community-contributed add-ons).

    Comment


    • #3
      Thank you very much

      Comment

      Working...
      X