Announcement

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

  • Generate new variable based on substring of two variables in loop

    Dear Statalist,

    I'd like to create a set of new variables based on the substring of two sets of variables in a loop. The manual version of what I have is
    Code:
    sort state 
    by state: gen dif_sum_temp = hi_sum_temp - lo_sum_temp 
    by state: gen dif_wint_snowfall = hi_wint_snowfall - lo_wint_snowfall 
    by state: gen dif_wind_force = hi_wind_force - lo_wind_force
    I have this process for about 20 variables, and I think there should be a way to do it in a loop since it's the same pattern. I think it could be done with the strpos()-, -substr()- or-subinstr()- options, but I am not quite sure how to do it. Below a dataex

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str3 state byte lo_sum_temp int hi_sum_temp byte(lo_wint_snowfall hi_wint_snowfall lo_wind_force) int hi_wind_force
    "CA"  80 130 20  80 0 199
    "DE " 75 100 12  70 0 200
    "NY"  70 115 30 100 0 231
    end

    Thank you for your help!



  • #2
    Code:
    foreach v of var lo* {
    local s = substr("`v'",3,.)
    bys state: gen diff`s' = hi`s' - lo`s'
    }

    Comment


    • #3
      Dear Oyvind, Thank you for your response! Does this code work because "hi" and "lo" are the same number of characters? Is that what the 3 does? What if I had "high" and "low" instead? Thank you!

      Comment


      • #4
        Correct. With "high" and "low",
        Code:
        foreach v of var low* {
        local s = substr("`v'",4,.)
        bys state: gen diff`s' = high`s' - low`s'
        }

        Comment


        • #5
          That worked perfectly, thank you very much. If I wanted to label the variables, what would be the best way to go about that? Is there a similar way to call upon the position based on variable label?

          For example, imagine all of my "hi" variables are already labeled "High in Summer Temperature" "High in Winter Snowfall" "High in Wind Force". Is there a way to specify the position of the "High in" and replace it with "Low in" and "Difference in"?

          To get the variable label:
          Code:
          label variable hi_wind_force "High in Wind Force" 
          label variable hi_sum_temp "High in Semmer Temperature" 
          label variable hi_wint_snowfall "High in Winter Snowfall"
          and then apply something like this?
          Code:
          label variable lo_sum_temp "Low in Summer Temperature" 
          label variable diff_sum_temp "Difference in Summer Temperature" 
          label variable lo_wint_snowfall "Low in Winter Snowfall"
          Perhaps this is straying too far from the topic -- let me know if I should make a different thread instead.
          Thank you in advance!

          Comment


          • #6
            Code:
            foreach v of var hi* {
            local lab : variable label `v'
            local s0 = substr("`v'",3,.)
            local s1 = substr("`lab'",5,.)
            la var lo`s0' "Low `s1'"
            la var diff`s0' "Difference `s1'"
            }

            Comment

            Working...
            X