Announcement

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

  • Renaming variables using observations and handling duplicates

    Hi,

    I'm interested in renaming variables using values from the first observations. The problem with the particular data set I'm currently working on is that in certain cases the strtoname() will evaluate to already existing variable. The problem may be illustrated with the code below:
    Code:
    // Create data set
    clear
    input str20 A str20 B
    "This is my desired name" "This is my desired name"
    "9098" "8676878"
    end
    // Rename
    foreach nvar of varlist A - B {
        rename `nvar' `=strtoname(`nvar'[1])'
    }
    What I would like to achieve is to accommodate for that possible and when needed attach some prefix or suffix to the variable. I was thinking of adding some counter but I will be grateful for suggestions how this can be neatly addressed? Bonus question, can I save original observation value as a note via something like:
    Code:
    notes `nvar' : Original text `nvar'[1]
    Kind regards,
    Konrad
    Version: Stata/IC 13.1

  • #2
    I'd suggest something like this:

    Code:
    local j = 0
    foreach v of var <varlist> {
         local ++j
         capture {
              rename `v' `=strtoname(`v'[1])'
              label var `=strtoname(`v'[1])'  "`= `v'[1]'"
         }
         if _rc {
              rename `v' var`j'
              label var var`j' "`=`v'[1]'"
         }
    }
    Last edited by Nick Cox; 07 Apr 2015, 11:01.

    Comment


    • #3
      Also see extended function permname.

      Code:
      // Create data set
      clear
      input str23 A str23 B // note str23
      "This is my desired name" "This is my desired name"
      "9098" "8676878"
      end
      
      // rename
      foreach var of var A-B {
          loc original_text : di `var'[1]
          loc newname = strtoname(`"`original_text'"')
          loc newname : permname `newname'
          ren `var' `newname'
          char `newname'[original_text] `"`original_text'"'
      }
      
      d ,f
      l
      char l
      Best
      Daniel
      Last edited by daniel klein; 07 Apr 2015, 15:55.

      Comment


      • #4
        Nick/Daniel,

        Thanks very much for your comments. permname seems like a nice gadget designed precisely for that purpose. What does it do exactly, I couldn't find much about it? I mostly googled Daniel's old posts on solving renaming problems with use of thepermname.
        Kind regards,
        Konrad
        Version: Stata/IC 13.1

        Comment


        • #5
          I remember seeing code conceptually along the lines

          Code:
          capture confirm new variable `suggested'
          if (_rc) {
              forvalues j = 1/32000 {
                  loc newname `suggested'`j'
                  capture confirm new variable `newname'
                  if !(_rc) continue ,break
              }
          }
          somewhere, but cannot remember exactly. My guess is that permname does something similar.

          Best
          Daniel

          Comment


          • #6
            I wouldn't Google this kind of detail. I would look directly at the documentation.

            http://www.stata.com/help.cgi?extended_fcn

            Comment


            • #7
              I was thinking how it handles duplicates, as the help entry reads:
              permname suggested_name [, length(#) ] returns a valid new variable name based on suggested_name in matname, where suggested_name must follow naming conventions but may be too long or correspond to an already existing variable.
              Either way, I find it very useful.
              Kind regards,
              Konrad
              Version: Stata/IC 13.1

              Comment

              Working...
              X