Announcement

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

  • Create variables with loop

    Hi, i would like to create some variables with loop.
    I have 5 variables : switch1 / switch2 /.../ switch5 and i would like to create variables like that :
    first_switch is the first switch that is not missing
    second_switch is th second switch that is not missing (and there is a first)
    third_switch is the third switch that is not missing (and there is a first and a second)




    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float ID str4(switch1 switch2 switch3 switch4 switch5)
    1 "" "W94" "W144" "W144" ""    
    2 "" "" "W94"  "W144" ""    
    3 "W94" "W144" "" "W192" ""    
    4 "" "" "W144" "" "W192"
    5 "W144" "" "W144" "" ""    
    6 "" "" "" "W192" ""    
    7 "W94" "" "" "W192" ""    
    8 "W144" "" "" "" "W192"
    9 "" "W144" "" "" "W192"
    10 "" "" "W144" "W192" ""    
    end
    and i would like to get that :

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float ID str4(switch1 switch2 switch3 switch4 switch5 first_switch second_switch third_switch)
    1 "" "W94" "W144" "W144" "" "W94" "W144" "W144"
    2 "" "" "W94" "W144" "" "W94" "W144" ""    
    3 "W94" "W144" "" "W192" "" "W94" "W144" "W192"
    4 "" "" "W144" "" "W192" "W144" "W192" ""    
    5 "W144" "" "W144" "" "" "W144" "W144" ""    
    6 "" "" "" "W192" "" "W192" "" ""    
    7 "W94" "" "" "W192" "" "W94" "W192" ""    
    8 "" "" "" "" "" "" "" ""    
    9 "" "W144" "" "" "W192" "W144" "W192" ""    
    10 "" "" "W144" "" "" "W144" "" ""    
    end
    Can i do it with a loop (i have so many values)?

    Thanks you

  • #2
    I don't have a very elegant way to code it, but this should do the job. It sneaks a unique symbol onto the code, concatenates them, and the splits them back by the order of appearance.

    Code:
    forvalues x = 1/5{
        gen temp`x' = switch`x' + "-"
        replace temp`x' = "" if temp`x' == "-"
    }
    
    gen allswitch = temp1 + temp2 + temp3 + temp4 + temp5
    split allswitch, gen(wanted) p(-)
    
    drop temp* allswitch

    Comment


    • #3
      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input float ID str4(switch1 switch2 switch3 switch4 switch5)
      1 "" "W94" "W144" "W144" ""    
      2 "" "" "W94"  "W144" ""    
      3 "W94" "W144" "" "W192" ""    
      4 "" "" "W144" "" "W192"
      5 "W144" "" "W144" "" ""    
      6 "" "" "" "W192" ""    
      7 "W94" "" "" "W192" ""    
      8 "W144" "" "" "" "W192"
      9 "" "W144" "" "" "W192"
      10 "" "" "W144" "W192" ""    
      end
      
      egen SW = concat(switch*), p(" ")
      
      forval j = 1 /3 {
          gen SW`j' = word(all, `j')
      }
      
      list all SW?
      
           +-------------------------------------+
           |            SW   SW1    SW2    SW3 |
           |-------------------------------------|
        1. |  W94 W144 W144    W94   W144   W144 |
        2. |       W94 W144    W94   W144        |
        3. | W94 W144  W192    W94   W144   W192 |
        4. |     W144  W192   W144   W192        |
        5. |     W144  W144   W144   W144        |
           |-------------------------------------|
        6. |           W192   W192               |
        7. |     W94   W192    W94   W192        |
        8. |   W144    W192   W144   W192        |
        9. |    W144   W192   W144   W192        |
       10. |      W144 W192   W144   W192        |
           +-------------------------------------+
      Or use split instead of the loop.

      Code:
      split SW



      In short, no loops are needed, as

      Code:
      egen SW = concat(switch*), p(" ")
      split SW 
      should get you there.
      Last edited by Nick Cox; 18 Oct 2021, 08:31.

      Comment


      • #4
        #2 works really well for my data. I'll try your code

        Thanks you

        Comment


        • #5
          I have to be puzzled here. There is no real difference between the results of these approaches. So, there is no obvious sense why one is said to work "really well" rather than another. Also, why choose longer code when you don't need it or a loop when you don't need one?

          Comment


          • #6
            Originally posted by Nick Cox View Post
            I have to be puzzled here. There is no real difference between the results of these approaches. So, there is no obvious sense why one is said to work "really well" rather than another. Also, why choose longer code when you don't need it or a loop when you don't need one?
            I'd agree with Nick. As I said in #2 I didn't know any better way to do it so it was a bit "brute force." If you can avoid loop, you probably should.

            Comment


            • #7
              To be honest, i didn't see your second code at #3, and it's true i was focused on a loop but i don't really need it

              Thanks you, have a good day

              Comment

              Working...
              X