Announcement

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

  • Count frequency of observation within a variable

    Hi,

    I would like to count the frequency of an observation within a variable.
    Thus, I would like to create a variable "a" which refers to variable"x" in the following pattern:

    variable "x" 1 1 1 2 2 2 2 2 6 7 7 7 8 8
    variable "a" 3 3 3 5 5 5 5 5 1 3 3 3 2 2

    Basically "a" counts how many times "x" is 1, how many times "x" equals 2 etc.

    Does someone know how to do that?
    I was unable to find anything in that direction..

    Thanks,
    Xueqian
    Last edited by Xueqian Chen; 16 Aug 2015, 09:21. Reason: count frequency observation

  • #2
    See -contract-.

    Comment


    • #3
      Say your original dataset is called mydata, then

      Code:
      tempfile mycopy
      save `mycopy'
      contract x , freq(a)
      mer 1:m x using `mycopy'
      will do what you want.

      Best
      Daniel

      Comment


      • #4
        All you need is to group observations by x and use _N (see help _variables) as the count. Also see [U] 11.5 by varlist: construct and

        SJ-2-1 pr0004 . . . . . . . . . . Speaking Stata: How to move step by: step
        . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . N. J. Cox
        Q1/02 SJ 2(1):86--102 (no commands)
        explains the use of the by varlist : construct to tackle
        a variety of problems with group structure, ranging from
        simple calculations for each of several groups to more
        advanced manipulations that use the built-in _n and _N
        Code:
        * Example generated by -dataex-. To install: ssc install dataex
        clear
        input byte x
        1
        1
        1
        2
        2
        2
        2
        2
        6
        7
        7
        7
        8
        8
        end
        
        * the number of observation in a group is _N
        bysort x: gen a = _N
        
        list, sepby(x)
        Last edited by Robert Picard; 16 Aug 2015, 10:10.

        Comment


        • #5
          Thanks to all of you! The contract command followed by the merge command works perfectly.
          I was not really able to use Robert's example as I have over 15.000 observations so that the code would be very long and unwieldy.

          Comment


          • #6
            It's just a single line, please read the references I just added. All you need is

            Code:
            bysort x: gen a = _N

            Comment


            • #7
              Oh, I got it now. I was thinking that I would need the upper part of your code as well, but it also works with a variable which is already in the dataset. Sorry for the confusion!

              Comment

              Working...
              X