Announcement

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

  • help: Creating new variable and populating it with total(var)

    Never mind! I got it with egen newvar = total(var), by(id)

    I have version 16.

    I have a data set where each ID is entered 10 times, then for 10 weeks answer yes/no (coded as 1/0) question. I need to create a new variable and then populate it with the number of times the id answers yes.

    I know I can generate the number of "yes" per id using
    total variable, over(id)

    I can then:
    gen newvar =.
    replace newvar = # if id == [id]


    doing that 50 time for each of the IDs doesn't seem efficient -- is there a better way to do this?
    Last edited by Hillary Connolly; 17 Jun 2021, 10:46.

  • #2
    Welcome to Statalist.

    I'm glad you were able to solve the problem yourself. For future reference, note that you accidentally posted your topic in Statalist's Mata Forum, which is used for discussions of Stata's Mata language, which is different than Stata's command language, and different than Stata's matrix commands. Your question would have seen a more appropriate, and much larger audience if you had posted it in Statalist's General Forum.

    Comment

    Working...
    X