Announcement

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

  • Generating new var conditional on other variables

    Hello, I am trying to generate a new variable (let's call it varNEW) based on 6 other variables (let's call them var1, var2, var3, var4, var5, var6).

    These 6 variable have values between -1 and 4.

    I want varNEW to be, for each observation, the count of "1" in the 6 other variables for this observation. How could I get to that? I'm guessing with a combination of count and if but I am struggling to get it to work.

    Just beginning with Stata, sorry for such a basic question. I didn't find an answer already, even though I'm sure it must be somewhere obvious...

    Thanks in advance for the help

  • #2
    not sure I completely understand, and there is no data example (see the FAQ), but -egen- with the "anycount" function is one possibility; see
    Code:
    help egen

    Comment


    • #3
      My guess is like Rich's, to wit

      Code:
      egen any1 = anycount(var1  var2  var3 var4 var5 var6), values(1)
      Obvious enough -- or it will be with a little more experience -- but do use names like any1 that mean something. It's really hard to remember what varNEW might mean.

      If your previous experience is with say a spreadsheet and you are used to column labels like A B C it takes a little while to get used to variable names.

      Comment


      • #4
        That was helpful indeed thanks I could achieve what I needed which was

        . egen newvar = anycount (varlist), values (1)

        Thanks a lot, I will get the reflex to consult the help command as of now!

        Comment

        Working...
        X