Announcement

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

  • Add blank space before specific word in label var

    Hi all,

    I am trying to add blank space for some specific word (Cost) when exist typo in label var, example of my data:

    Code:
    clear all
    
    input cost1 cost2 cost3 cost4
    . . . .
    end
    label var cost1 "TotalCost Machinery1"
    label var cost2 "VarCost Machinery2"
    label var cost3 "RandomCost Machinery3"
    label var cost4 "ExternalCost Machinery4"
    My goal is insert a blank space before the word: "Cost" in label var that have a wrong label var: cost1, cost2 and cost4.

    Click image for larger version

Name:	Sin título.png
Views:	1
Size:	13.8 KB
ID:	1665046


    I tried with:

    Code:
    foreach var of varlist * {
        local varlabel : var label `var'
        local newname = regexr("`varlabel'", "(.)+\Cost", " Cost")
        label variable `var' "`newname'"
    }
    But only split the var label:

    Click image for larger version

Name:	Sin título.png
Views:	1
Size:	13.2 KB
ID:	1665047


  • #2
    Here is one way:
    Code:
    clear all
    
    input cost1 cost2 cost3 cost4
    . . . .
    end
    label var cost1 "TotalCost Machinery1"
    label var cost2 "VarCost Machinery2"
    label var cost3 "RandomCost Machinery3"
    label var cost4 "ExternalCost Machinery4"
    
    forv i = 1/4 {
        local a: variable label cost`i'
        local b = subinstr("`a'", "Cost", " Cost",.)
        label variable cost`i' "`b'"
    }
    describe

    Comment


    • #3
      Thanks Scott,

      Was so easy, My focus was wrong using regexr.

      Thanks

      Comment


      • #4
        It does not add anything to solve the core problem, but elabel (SSC, SJ, GitHub) could conveniently replace the loop:

        Code:
        elabel variable (*) (=subinstr(@, "Cost", " Cost", .))

        Comment


        • #5
          Thanks Daniel for you reply. Its true "elabel" always fix all the problems.

          Comment

          Working...
          X