Announcement

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

  • string variable: How to substract one string and add to another one, with a condition?

    Hi everyone,

    I would like to do a simple operation. Basically, I want to substract one string from a variable, and include it in another one.
    Here is a dataex for more clarity:


    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str5(CO2gkm cv)
    "148 1" "70"
    "153 1" "70"
    "155 1" "35"
    "150 1" "40"
    "155 1" "60"
    "150 1" "60"
    "155 1" "60"
    "150 1" "60"
    "139 1" "80"
    "134 1" "80"
    end

    Basically, for all string values in variable "CO2gkm" following this pattern: "xxx x", I want to take the last "x" and add it to the "cv" values, at the beginning of the string.
    So, for example in my data example, in the first line, I would like to extract the last "1" from CO2gkm, and add it to the beginning of "70" in cv, to have "170", and so on for other variables.

    Could anyone give me some insights about that, please?

    Thank you in advance!
    Best,

    Michael

  • #2
    Code:
    replace cv = substr(CO2gkm,strlen(CO2gkm)-1,.) + cv

    Comment


    • #3
      Code:
      replace cv = ustrright(CO2gkm,1) + cv

      Comment


      • #4
        Hi George Ford,

        That was exactly what I wanted. Thank you so much for your work.
        Lovely day.

        Michael

        Comment

        Working...
        X