Announcement

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

  • Remove combination of quotes and asterisk in variable label

    Hi all,

    I have a extrange combination of quotes and asterisk in variable label in my data base... Lamentably I can not remove asterisk and/or quotes in variable label.


    I am using a loop because this "kind" of label repeat in diferents variables. I tried with differents combinations:

    Example:

    Code:
    clear all
    set more off
    sysuse auto
    
    label var foreign `""*Fool label*""'
    
    local varlabel : var label foreign
    local newname = subinstr("`varlabel'","*","",.)
    label variable `var' "`newname'"
    
    
    Foollabel not found
    
    
    local varlabel : var label foreign
    local newname = subinstr("`varlabel'",`"*',"",.)
    label variable `var' "`newname'"
    
    
    too few quotes
    Please, any comments I would gratefull.
    Regards

  • #2
    There are two problems with both of your approaches.

    Code:
    [...]
    local varlabel : var label foreign
    local newname = subinstr("`varlabel'","*","",.)
    label variable `var' "`newname'"
    The most obvious problem is that local macro `var' is not defined; it needs to be replaced with foreign. I assume that the local is defined within your loop, which you do not show us. The second problem is that you must use compound double quotes, i.e. `" and "' if the string (in this case: variable label) that you are referring to contains double quotes. You want

    Code:
    local newname = subinstr(`"`varlabel'"',"*","",.)
    label variable `var' `"`newname'"'
    (assuming that `var' is actually defined)


    Note that elabel (SSC) can solve this problem.

    Code:
    elabel variable (foreign) (= subinstr(@, "*", "", .))
    elabel variable (foreign) (= subinstr(@, `"""', "", .))
    The loop is implied; just add more variables to foreign in the first pair of parentheses.

    Comment


    • #3
      Thanks daniel klein for you reply. Sorry for my mistake... I should use the loop like example...

      Your code run great, it solution my problem:

      Code:
      foreach var of varlist _all {
      
      local varlabel : var label `var'
      local newname = subinstr(`"`varlabel'"',"*","",.)
      label variable `var' `"`newname'"'
      
      }
      Regards
      Rodrigo

      Comment

      Working...
      X