Announcement

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

  • Error renaming variable: old variable includes a symbol

    Hi all,

    I am trying to rename a variable that stands for annual income on the Spanish Survey of Household Finances, and an unexpected problem has arisen. I have tried to rename the target variable both by writing the rename command and also manually, but it always gives me an error, as if that variable is not found. The problem is due to the symbol on the original variable name, which allegedly Stata cannot detect (I am using Stata 16). I am attaching a screenshot of the problem.
    Click image for larger version

Name:	thumbnail_image.png
Views:	1
Size:	2.9 KB
ID:	1612966




    Do you have any suggestions on how to proceed?

    Best regards,
    Last edited by Borja Urrea; 03 Jun 2021, 10:19.

  • #2
    There are workarounds (see program code below). However, I suggest you read

    Code:
    help unicode translate
    and fix the file's encoding.


    Code:
    *! version 1.0.0 23jan2020
    program legalnames
        version 11.2
        
        syntax          // nothing allowed
        
        if ( !c(k) ) exit // nothing to do
        
        mata : legalnames()
    end
    
    version 11.2
    
    if (c(stata_version) >= 14) local u u
    
    mata :
    
    mata set matastrict on
    
    void legalnames()
    {
        real   scalar i
        string scalar name
        
        for (i=1; i<=st_nvar(); ++i) {
            if ( st_isname(st_varname(i)) ) 
                continue
            else 
                name = `u'strtoname(st_varname(i))
            if ( !_st_varindex(name) )
                st_varrename(i, name)
            else if ( !_stata("local name : permname " + name) )
                st_varrename(i, st_local("name"))
            else 
                printf("note: could not rename %s\n", st_varname(i))
        }
    }
    
    end
    exit

    Comment


    • #3
      That was super useful, thank you Daniel.

      Comment

      Working...
      X