Announcement

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

  • Generating a variable whose values are the total number of times a value exists in another variable

    Hello to everyone,

    I want to generate a new variable that is the sum of the number of times another variable takes on a certain value. I can use the "count if" command, and I know that the value 351 appears 211 times in my ID variable. So I want to create a new column that will have the value 211 every time ID is equal to 351. And I know the value of 355 appears 494 times in my ID column, so I want my new variable column to have the value of 494 every time ID is equal to 355.

    So if my data was the below, I would want it to look like this:
    ID: newvar
    355 3
    351 2
    356 1
    355 3
    358 1
    359 2
    351 2
    357 1
    355 3
    Since 355 appears 3 times, the newvar variable takes on the value of "3" everytime ID is equal to 355. And since 351 appears twice, newvar takes on the value of 2 every time ID is equal to 351. Unfortunately my is data large, with over 1,250 observations. Any assistance would be greatly appreciated.

  • #2
    Code:
    bys id: egen wanted=count(id) if !missing(id)

    Comment


    • #3
      Oh wow. Andrew, thank you so much. I was not aware of the bysort function. This has been a great learning experience. Thank you again

      Comment


      • #4
        Code:
        bysort id : gen wanted = _N
        is another way to do it.

        Comment

        Working...
        X