Announcement

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

  • Creating a sum of variables when there are no missing values (.)

    Hello all, I'm a relatively new Stata user.

    I have a set of variables that are the responses to a survey, and I need to create a variable that sums up the score. This wouldn't be a problem if my variables were set up as "traditional" dummy variables (1=correct, 0=not correct, .=missing/refusals) as I would use
    Code:
    egen total=rowtotal(var1 var2 var3 var4 var5)
    but unfortunately they are coded differently: 1=correct, 2=not correct, while missing/refusals are -9 and -8. Of course, if I use the command above, I get wonky results that don't range between 0 and 5 like I want them to.

    Is there any way I can create my sum variable, without having to recode all the variables (so that 1=correct, 0=not correct, .=missing/refusals)?

    Thank you for your help.

  • #2
    Code:
    egen total = anycount(var1 var2 var3 var4 var5), values(1)
    If your desire to avoid recoding is that it seems tedious, note that you can recode a varlist in a single command:
    Code:
    recode var1 var2 var3 var4 var5 (2=0) (-8=.a) (-9 = .b)
    egen total = rowtotal.....

    In general, -egen- has many useful commands, so many that I rarely remember them. As a new user, I'd suggest always looking at -egen- if you can't figure out how to do something.

    Comment


    • #3
      Luna:
      welcome to this forum.
      You can recode your variables by looping over them:
      Code:
      for each var of varlist var1-var5  {
       replace `var'=. if `var'==-9 | `var'==-8
       replace `var'=0 if `var'==2
        }
      PS: crossed in the cyberspace with Mike's reply, that looks smarter than mine.
      Kind regards,
      Carlo
      (Stata 18.0 SE)

      Comment


      • #4
        There is usually more than one way to do it and here is another:

        Code:
        foreach var of var var1-var15 {
             replace `var' = cond(`var' == -8, .a, cond(`var' == -9, .b, 2 - `var'))
        }
        which is easier to understand if made verbal

        if the variable is -8 then return .a
        otherwise if -9 then return .b
        otherwise return 2 - value

        as 2 - value maps 2 to 0 and 1 to 1.
        Last edited by Nick Cox; 21 Jun 2018, 09:01.

        Comment


        • #5
          Originally posted by Carlo Lazzaro View Post
          You can recode your variables by looping over them
          Originally posted by Nick Cox View Post
          There is usually more than one way to do it and here is another:

          Code:
          foreach var of var var1-var15 {
          replace `var' = cond(`var' == -8, .a, cond(`var' == -9, .b, 2 - `var'))
          }
          which is easier to understand if made verbal

          if the variable is -9 then return .a
          otherwise if -9 then return .b
          otherwise return 2 - value

          as 2 - value maps 2 to 0 and 1 to 1.
          Probably loops are the best idea as the variables I need to recode are quite a lot. I'm not really familiar with loops but your code is certainly going to help.

          Given that in practice my variables are not named var* but w1var_a, w1var_b, w2var_a, etc. (my dataset contains several waves), would your code work with
          Code:
          foreach var of varlist w*var_a
          (and then repeating it for value B, C, etc)?
          Last edited by Luna Minerva; 21 Jun 2018, 08:40.

          Comment


          • #6
            Luna:
            yes, it should work.
            Kind regards,
            Carlo
            (Stata 18.0 SE)

            Comment


            • #7
              Thank you!

              Comment

              Working...
              X