Announcement

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

  • Replace Spanish letters in variable name

    hello

    i need to use the year variable but in spanish it has the letter Ñ, so its a�o. ive tried to rename it but i keep getting the r(111) error which tell me that the variable doesnt exist. ive also tried to export tha database to excel but i cant get the values correct in a sheet. how can i rename it or fix this?

    thank you

  • #2
    Code:
    rename  rename a`=uchar(209)'o ano
    That's for the upper case Ñ. If you have the lower case version, instead of 209, it would be 241.

    Comment


    • #3
      hello clyde! i still get the same error, i think its because the variable is not name año but a�o, with that symbol. thank you

      Comment


      • #4
        The following should do it provided that the variable is the only one that starts with an "a" and ends with an "o". Otherwise, use dataex to output the exact character.


        Code:
        ds a*o
        assert wordcount("`r(varlist)'")==1
        rename `r(varlist)' ano

        Comment


        • #5
          I wrote a tiny utility that can help find variable names using regular expression patterns. Search for -findregex- on SSC (-search findregex-).

          The example below performs a case-insensitive serach for the accented "ñ" or "Ñ" and replaces all occurrences found in any variable with a lowercase "n".

          Code:
          set obs 1
          gen int año = 2023
          describe
          
          findregex , re("Ñ")
          sreturn list
          
          foreach v of varlist `s(varlist)' {
            rename `v' `=ustrregexra("`v'", "ñ", "n", .)'
          }
          
          describe
          Relevant output

          Code:
          . describe
          
          ----------------------------------------------------------------------
          Variable      Storage   Display    Value
              name         type    format    label      Variable label
          ----------------------------------------------------------------------
          año             int     %8.0g                 
          ----------------------------------------------------------------------
          
          . describe
          
          ----------------------------------------------------------------------
          Variable      Storage   Display    Value
              name         type    format    label      Variable label
          ----------------------------------------------------------------------
          ano             int     %8.0g                 
          ----------------------------------------------------------------------

          Comment


          • #6
            https://www.statalist.org/forums/for...14#post1493114

            Comment

            Working...
            X