Announcement

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

  • changing variable labels

    I have a set of variables with names such as "tibia_epiCSA1" where the last character is sometimes a "2" rather than a "1"; I want to change the current variable labels (which come from a -reshape- command) to have "start" as the first word if the last character in the varname is "1" (or to have "end" as the first word of the variable label if the last character in the varname is "2") and then to copy the variable name except that I want to exclude the last character (the "1" or the "2"); I get part way via
    Code:
    local mac1 tibia_epiCSA1
    la var tibia_epiCSA1 "start `mac1'"
    but this still leaves me the ending digit

    open to a different strategy or to a follow-up command that will get rid of the digit

    I note that I have several dozen such variables so want a general way to do this

  • #2
    Code:
    foreach var of varlist tibia_* {
        local last = substr("`var'",-1,1)
        local allbutlast = substr("`var'",1,length("`var'")-1)
        if "`last'" == "1" local first start
            else local first end
        label var `var' "`first' `allbutlast'" 
    }

    Comment


    • #3
      thank you very much - since some of my variables refer to other bones (e.g., femur) but follow exactly the same structure, I just added, e.g., "femur-*" to the first line of your code and it worked great

      Comment

      Working...
      X