Announcement

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

  • Variable counting the number of times another variable takes on a certain value.

    Hello,

    I have data which looks similar to this:

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float(isic3_3d class)
    151 1
    151 2
    151 2
    151 1
    151 2
    151 2
    151 1
    151 2
    151 2
    151 1
    151 2
    151 2
    151 1
    151 1
    151 2
    151 2
    151 1
    151 1
    151 2
    151 2
    151 2
    151 1
    151 2
    151 2
    151 2
    151 2
    151 2
    151 2
    151 2
    151 2
    151 2
    151 2
    151 2
    151 2
    151 2
    151 2
    151 2
    151 2
    151 2
    151 2
    151 2
    151 2
    151 2
    151 2
    151 2
    151 2
    151 1
    151 2
    151 2
    151 2
    151 2
    151 2
    151 2
    151 2
    151 2
    151 2
    151 2
    151 2
    151 2
    151 2
    151 2
    151 2
    151 2
    151 2
    151 2
    151 2
    151 2
    151 2
    151 2
    151 2
    151 2
    151 2
    151 2
    151 2
    151 2
    151 2
    151 2
    151 2
    151 2
    151 2
    151 1
    151 2
    151 2
    151 2
    151 2
    151 2
    151 2
    151 2
    151 2
    151 2
    151 2
    151 2
    151 2
    151 2
    151 2
    151 2
    151 2
    151 2
    151 2
    151 2
    end
    label values class x
    label def x 1 "Intermediate", modify
    label def x 2 "Consumption", modify
    In this sample of my data there is only one value for isic3_3d, but in my data I have many values for this variable, here it only ever equals 151. I would like to create a variable which indicates the number of times the isic3_3d observation takes on the value 1 for class, when doing a simple count it would look like this:

    Code:
    count if isic3_3d==151 & class==1
    Whilst it's not illustrated here, I have many values of isic3_3d so I would like to create a variable which calculates the number of times class==1 by the variable isic3_3d. So for example, for all entries with isic3_3d ==151 this variable would take on the same value and it would take the value of "count if isic3_3d==151 & class==1". Then for all entries where isic3_3d equals say 152, this variable would take on the value of "count if isic3_3d==152 & class==1".

    Please let me know how I can implement this.

    Thanks,
    Jad

  • #2
    Code:
    egen countvar= count(cond(class==1,isic3_3d,.)), by(isic3_3d)

    Comment


    • #3
      Code:
      egen wanted = total(class == 1), by(isic3_2d)
      See also https://journals.sagepub.com/doi/pdf...867X1101100210 for some other tricks in this territory.

      Comment

      Working...
      X