Announcement

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

  • Removing numbers from text in labels

    Hi, I used sencode command in order to display the label's codes and text in the variable labels (in Hebrew).
    sencode made the variable type numaric.
    Now, I need to remove the numbers and keep only the text (to display in the graph)
    I tried to run a loop but it is with string and of course it does not work.
    What can I do? (sorry for the opposite table I struggled with the copy-past here).


    Code:
    sencode occupation_group, gene(occupation_group_name1_cat)
    Code:
    foreach i of varlist ocupation_group_name1_cat {
    local a : variable label `i'
    local a: subinstr local a  `.'
    label var `i' ""
    }

    occupation_group_name1_cat| Freq. Percent Cum.
    ----------------------------------------+-----------------------------------
    כלכלנים כלליים. 123 | 8 1.25 1.25
    מזכירות ומנהלות לשכה.124 | 32 4.99 6.24
    סטטיסטיקנים כללי.125 | 5 0.78 7.02
    עובדי אמרכלות.126 | 24 3.74 10.76


  • #2
    It's possible to use regular expressions as in

    Code:
    foreach var of varlist *{
        lab var `var' "`=ustrregexra("`:var lab `var''", "\d+|\.", "")'"
    }

    ADDED IN EDIT: I think you are mixing up value labels and variable labels. See https://www.statalist.org/forums/for...of-value-label.

    Code:
    sysuse auto, clear
    *ADD LEADING DIGITS
    lab def origin 0 "123.Domestic", modify
    lab def origin 1 "For11.eign213", modify
    lab list
    *START HERE
    levelsof foreign, local(levels)
    foreach l of local levels{
        lab def `:val lab foreign' `l' "`=ustrregexra("`:lab (foreign) `l''", "\d+|\.", "")'", modify
    }
    lab list
    Res.:

    Code:
     lab list
    origin:
               0 123.Domestic
               1 For11.eign213
    
    
    .
    . lab list
    origin:
               0 Domestic
               1 Foreign
    Last edited by Andrew Musau; 16 Nov 2022, 02:30.

    Comment


    • #3
      Amazing!!! Thank you! It works

      Comment


      • #4
        Isn't this what numlabel offers through an option?

        Comment


        • #5
          numlabel might work for the problem as reported in #1. However, numlabel only removes numeric prefixes that match the numeric code. Consider:

          Code:
          . label list
          foo:
                    42 42. foo
                    73 42. bar
          
          .
          . numlabel foo , remove force
          
          .
          . label list
          foo:
                    42 foo
                    73 42. bar
          The code in #2 will, conversely, strip any numbers, including potentially valid ones.

          Code:
          foo:
                    42 42. foo
                    73 16. November 2022
          
          (output omitted)
          . label list
          foo:
                    42  foo
                    73  November
          The code in #2 also relies on all values being present in a variable. The assumption probably holds because the variables were created using sencode (probably from SSC). In a more general setting, we can apply the same logic to value labels directly using elabel (e.g., SSC)

          Code:
          elabel define labelname (=#) (=ustrregexra(@, "\d+|\.", "")), modify

          Comment

          Working...
          X