Announcement

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

  • Transform multiple binary varibales into one single varibale

    Hello everybody,

    if i have a dateset which looks like this
    Individual Var1 Var2 Var3
    1 1 0 0
    2 0 0 1
    3 1 0 0
    4 0 1 0
    5 0 0 1

    what steps do i have to take to generate a new varibale (newVar) which shows me..
    .
    Individual newVar
    1 Var1
    2 Var3
    3 Var1
    4 Var2
    5 Var3

    Thank you very much in advance


  • #2
    If there any no occurences where a single individual has a '1' for more than one Var, you can simply do this:

    Code:
    gen newVar = "Var1" if Var1 == 1
    replace newVar = "Var2" if Var2 == 1
    replace newVar = "Var3" if Var3 == 1
    If this is the right solution depends on how your data looks in more detail. If you have many vars, it would be better to use a loop for example. Also, do you want newVar to have to actual value of the variables called 'Var1', 'Var2' and 'Var3' or just have them say 'Var1'? Not much detail is clear from your post in that regard.
    Last edited by Jesse Tielens; 28 Jul 2018, 07:41.

    Comment


    • #3
      Hi Jesse,

      first of all, thank you very much with your help.

      Indeed. i want newVar to just display "Var1", "Var2" and "Var3".

      So the actual data should look something like this
      Individual newVar
      1 1
      2 3
      3 1
      4 2
      5 3
      Afer that, i want to label those outcomes with whatever label i want (in this case Var1 Var2 Var3).


      And for your first question: Yes there are more than 3 variables im dealing with. In total there are 12 dummy varibles, so i guess using loop makes more sense.

      So how do i assign a specific value for each dummy varibale? Is it the same method as you described above? (The labeling after that should be no problem)

      Thank you very much in advance
      Last edited by Ahmad Wali; 28 Jul 2018, 08:04.

      Comment


      • #4

        Then you should be all set right? Repeat the code snippet above for all 12 variables. And use 'label define' command to label your variable.

        Comment


        • #5
          Or does Ahmad want a numeric variable instead of a string? If so, he can use the loop below, assuming his variable names are indeed Var1-Var12. If not, a more general loop could be created.


          Code:
          gen new_var=.
          forvalues i=1/12 {
              replace new_var=`i' if Var`i'==1
              lab define new_var `i' "Var`i'", modify
              }
          lab val new_var new_var
          Stata/MP 14.1 (64-bit x86-64)
          Revision 19 May 2016
          Win 8.1

          Comment


          • #6
            It worked. Thank you very much for the help, to both of you.

            @ Carole
            I picked the variable names just randomly. So it was just a coincidence. Thank you anyways for providing me the code for those cases.

            Comment


            • #7
              So, in that case:


              The following code should work if you replace the part in red with your list of 12 variables (or if they are arranged next to each other in the variable list, you can use: my_first_var-my_last_var):

              Code:
              gen new_var=.
              local i=1
              foreach var of varlist XXXXX {
                  replace new_var=`i' if `var'==1
                  lab define new_var `i' "`var'", modify
                  local ++i
                  }
              lab val new_var new_var
              Stata/MP 14.1 (64-bit x86-64)
              Revision 19 May 2016
              Win 8.1

              Comment

              Working...
              X