Announcement

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

  • Removing quotation marks when changing multiple label values

    Dear Statalists,

    I would like to add prefixes to all values across multiple label values.
    Specifically, I am customizing a dtable and am indenting categorical variables by a couple extra spaces using char(160).

    Here is my example code, adding "prefix_" to each value for clarity:
    Code:
    clear all
    sysuse bplong.dta
    
    local categ sex when
    
    foreach var of local categ {
        local valname : value label `var' // get name of value label for variable `var'
        quietly: label list `valname' // list variable labels
        
        local newlabel // reset local label
    
        forvalues i = `r(min)'/`r(max)' {
            local oldsinglelabel : label `valname' `i' // get next label value
            local newsinglelabel `i' `""prefix_`oldsinglelabel'""' // add prefix
            local newlabel `newlabel' `newsinglelabel' // local with new label values list
            }
    
        display "`newlabel'"
        
        label define `var'_newlabel `newlabel', replace
        label values `var' `var'_newlabel
            
        }
    The problem is that each new value label includes quotation marks like so:
    Code:
    label list
    
    when_newlabel:
               1 "prefix_Before"
               2 "prefix_After"
    sex_newlabel:
               0 "prefix_Male"
               1 "prefix_Female"
    when:
               1 Before
               2 After
    sex:
               0 Male
               1 Female
    I can't figure out how to remove these. There must be something wrong with my use of double quotation marks.

    Alternatively, is there a better way of further indenting value labels for categorical variables within dtable or using collect?

    Thank you for your time.

    Best regards,
    Wei

  • #2
    I do not fully understand the ultimate goal and I have not used dtable or collect a lot. I guess that the latter two commands are probably the place to adjust the layout.

    Anyway, for changing value labels, I will point to elabel (SSC, SJ, or GitHub).

    Code:
    . sysuse bplong.dta
    (Fictional blood-pressure data)
    
    . elabel define * (=#) (="prefix_"+@) , modify
    
    . label list
    when:
               1 prefix_Before
               2 prefix_After
    sex:
               0 prefix_Male
               1 prefix_Female
    agegrp:
               1 prefix_30-45
               2 prefix_46-59
               3 prefix_60+
    
    . 
    end of do-file

    Comment


    • #3
      Thank you Daniel! Your command perfectly fits my needs.
      It for some reason returned "# not allowed in eexp" when using it on my own data with variable names in elblnamelist but switching to value label names solved it.

      Always love it when a single line solves my problems.

      Just to explain. I wanted to change the variable name column in my descriptive table from this:

      Code:
      Background
       Age
       Gender
        Male
        Female
       CRP
      To this:
      Code:
      Background
       Age
       Gender
           Male
           Female
       CRP

      And figured adding spaces to the label values would be an easy solution to move the "Male" and "Female" further right since I couldn't find that dtable supports it natively.
      Last edited by Wei Hai Deng; 11 Mar 2024, 10:38.

      Comment


      • #4
        Originally posted by Wei Hai Deng View Post
        Thank you Daniel! Your command perfectly fits my needs.
        It for some reason returned "# not allowed in eexp" when using it on my own data with variable names in elblnamelist
        Well, elabel define expects a value label name (or a variable name, enclosed in parentheses). However, it does not require that the value label name refers to an already defined value label. If you feed it a name that does not match any defined value label, elabel define assumes you want to define a new value label. And, new value labels do not yet have any (integer) values to which the wildcard # could refer. Hence, you cannot use wildcards when defining new value labels. This is what the error message is pointing out.

        Comment


        • #5
          I see, I missed the fact that variable names need to be enclosed in parantheses to refer to the linked value label name.
          I couldn't figure out why the same code works on the blood-pressure data but not mine but now realize that the value label names are exactly the same as the variable names in bplong.dta, not so in my data.

          Thanks again for the explanation.

          Comment

          Working...
          X