Announcement

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

  • Rename from last part of label that includes "."

    Hello! I'm trying to rename variables from the label, but this label includes dots (".")

    Example Label: "aaa.1.bbb.actual_label"

    Hence, the following doesn't work:

    foreach v of varlist v655 v656 ... v688 v689 {
    local x : variable label `v'
    rename `v' `x'
    }

    I've used ways to read from a particular position in a string before but I can't seem to make it work now.

    Would be very grateful for help!

  • #2
    Code:
    foreach v of varlist v* {
        
        local x : variable label `v'
        local x = ustrregexrf("`x'", "^.*\.","")
        local x = strtoname("`x'")
        
        rename `v' `x'
        
    }

    Comment


    • #3
      and here is a way without using regular expressions:
      Code:
      . di subinstr("aaa.1.bbb.actual_label",".","_",.)
      aaa_1_bbb_actual_label
      see
      Code:
      help subinstr()

      Comment


      • #4
        One reg-ex-free altarnative to #2 is:
        Code:
        foreach v of varlist v* {
            
            local x : variable label `v'
            local x = usubstr("`x'", 1-(ustrpos(ustrreverse("`x'")), "."), .)
            local x = ustrtoname("`x'")
            
            rename `v' `x'
        }
        Code:
        clear
        input v1-v2
        . .
        end
        
        lab var v1 "aaa.1.bbb.actual_label" 
        lab var v2 "aaa.1.bbb.actual_label to" 
        
        foreach v of varlist v* {
            
            local x : variable label `v'
            local x = usubstr("`x'", 1-(ustrpos(ustrreverse("`x'")), "."), .)
            local x = ustrtoname("`x'")
            
            rename `v' `x'
        }
        
        describe
        Code:
        . describe
        
        Contains data
         Observations:             1                  
            Variables:             2                  
        ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
        Variable      Storage   Display    Value
            name         type    format    label      Variable label
        ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
        actual_label    double  %10.0g                aaa.1.bbb.actual_label
        actual_label_to double  %10.0g                aaa.1.bbb.actual_label to
        ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
        Sorted by: 
             Note: Dataset has changed since last saved.

        Comment

        Working...
        X