Announcement

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

  • loops

    Hi!

    I would like to create one single loop for these 3 sections... is that possible?
    Many thanks!


    gen lbWTP_ph = - AO_ph
    replace lbWTP_ph = 75 if AO_ph == -76
    replace lbWTP_ph = -1000 if AO_ph == 76

    gen lbWTP_pa = - AO_pa
    replace lbWTP_pa = 75 if AO_pa == -76
    replace lbWTP_pa = -1000 if AO_pa == 76

    gen lbWTP_pe = - AO_pe
    replace lbWTP_pe = 75 if AO_pe == -76
    replace lbWTP_pe = -1000 if AO_pe == 76


    As well as a second loop for these 3 sections:


    gen info_avoiding_ba = .
    replace info_avoiding_ba = 1 if WTP_ba < 0
    replace info_avoiding_ba = 0 if WTP_ba >= 0

    gen info_avoiding_be = .
    replace info_avoiding_be = 1 if WTP_be < 0
    replace info_avoiding_be = 0 if WTP_be >= 0

    gen info_avoiding_bh = .
    replace info_avoiding_bh = 1 if WTP_bh < 0
    replace info_avoiding_bh = 0 if WTP_bh >= 0

    Is that possible?

  • #2
    Compare your question at https://stackoverflow.com/questions/...loops-in-stata

    It's not the same question, but some of the ideas carry over.

    Comment


    • #3
      Thank you very much! Yes, indeed. I was thinking about running the code below (inspired by your link) but it does not work.
      gen lbWTP_ph = - AO_ph
      gen lbWTP_pa = - AO_pa
      gen lbWTP_pe = - AO_pe
      gen lbWTP_ba = - AO_ba
      gen lbWTP_be = - AO_be
      gen lbWTP_bh = - AO_bh

      foreach v in lbWTP_ph lbWTP_pa lbWTP_pe lbWTP_bh lbWTP_ba lbWTP_be {
      replace `v' = 75 if `-v' == -76
      replace `v' = -1000 if `-v' == 76
      }

      Thank you!

      Comment


      • #4
        You have an extra - in `v'. On the other hand, it might be easy to understand to iterate through ph pa pe
        Code:
        foreach v in ph pa pe bh ba be {
            replace lbWTP_`v' = 75 if AO_`v' == -76
            replace lbWTP_`v' = -1000 if AO_`v' == 76
        }
        Last edited by Daniel PV; 19 May 2022, 09:44.

        Comment


        • #5
          deleted

          Comment

          Working...
          X