Announcement

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

  • Count the number of a specific value in differnet variables

    Dear Honourable members of the forum,

    Probably my question is too easy for you, but I got stuck for the following task.
    My data configguration is like that
    Year id Var1 var 2 var3 New var
    2005 20051 1 0 0 1
    2005 20052 0 0 0 0
    2005 20053 0 1 1 2
    2005 20054 1 1 1
    2005 20055 1 1 1
    2005 20056 1 1 1
    2005 20057 0 1 1
    2005 20058 0 0 0
    2005 20059 1 0 1
    I would like to create a new variable that count of a specific value "=1" observed in the different variables from var 1 to var 3 for each subject. For example here, the subject with id 20053, have a score=2 for the new var because the number 1 was observed 2 times within the three studied variables (var1 to var 3).
    Thank you for your help.

  • #2
    See help egen, specifically the rowtotal() function.

    Comment


    • #3
      If var1/2/3 can only take the values 0/1. then use:
      Code:
      gen NewVar = Var1 + Var2 + Var3

      Comment


      • #4
        Thank you wouter and John

        please another question. if one of the column contains number with decimals what I should do?
        Year id Var1 var 2 var3 New var
        2005 20051 1 0.5 0.4 1
        2005 20052 0 0 0 0
        2005 20053 0 1 0.6 1
        2005 20054 1 1 1
        2005 20055 1 1 0.5
        2005 20056 1 1 1
        2005 20057 0 1 1
        2005 20058 0 0 0
        2005 20059 1 0 1

        Comment


        • #5
          Compare #2. Perhaps you seek the rowmax() function of egen.

          Comment


          • #6
            Code:
            gen new_var = .
            foreach var of varlist var1 var2 var3 {
               replace new_var = new_var + `var' if `var' == 1
            }

            Comment


            • #7
              Originally posted by Nick Cox View Post
              Compare #2. Perhaps you seek the rowmax() function of egen.
              Thank you Nick. But this function give me the highest value however, I would like that the new var contains the number of occurence of "1" in each variables

              Comment


              • #8
                Originally posted by Wouter Wakker View Post
                Code:
                gen new_var = .
                foreach var of varlist var1 var2 var3 {
                replace new_var = new_var + `var' if `var' == 1
                }
                Thank you Wouter but it doesn't work

                Comment


                • #9
                  first, please read the FAQ and follow its advice, esp re: use of -dataex-

                  second, I think you want the "anycount" function of -egen- with val(1); if you use -dataex- it is easy to test, and thus provide, exact code

                  Comment


                  • #10
                    Wouter Wakker 's code above would have been the answer as well, but it must initiate the new variable new_var with 0 (zero) not with a . (missing).

                    +1 for the anycount() function, which should be sufficient in this case.

                    Comment


                    • #11
                      Thanks Rich
                      Thanks Sergiy, Yes anycount worked perfectly.

                      Comment

                      Working...
                      X