Announcement

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

  • Truncating long variable names

    Hi,
    Some of the variables in my dataset are too long, and trying to rename them returns an error message. Is there any trick to get around this error?

    Code:
    ............invalid name
    r(198);

  • #2
    What do you mean by "are too long"? Do you mean you have a variable name in your dataset that exceeds Stata's built-in (and apparently unchangeable) limit of 32 characters? Or do you mean they are longer than you would like them to be? If they exceed the 32 character limit, how were they created?

    What was the precise command you issued that resulted in the error message you display in post #1?

    You apparently imagine that among the members of Statalist there will be at least one who reads your post and has experienced the problem you describe and can answer your question directly. That's a fairly limited pool of potential answerers, and you can expand that pool by providing an adequate description of your problem so that those who have not experienced it but are intrigued by the possibility can fully understand the problem and attempt to deduce a solution.

    Comment


    • #3
      Originally posted by William Lisowski View Post
      What do you mean by "are too long"? Do you mean you have a variable name in your dataset that exceeds Stata's built-in (and apparently unchangeable) limit of 32 characters? Or do you mean they are longer than you would like them to be? If they exceed the 32 character limit, how were they created?

      What was the precise command you issued that resulted in the error message you display in post #1?

      You apparently imagine that among the members of Statalist there will be at least one who reads your post and has experienced the problem you describe and can answer your question directly. That's a fairly limited pool of potential answerers, and you can expand that pool by providing an adequate description of your problem so that those who have not experienced it but are intrigued by the possibility can fully understand the problem and attempt to deduce a solution.
      Hi William,
      The data were prepared outside Stata. Here is the original command:

      Code:
      . rename MessageTraditionalMediaUnderstand  Item4
      MessageTraditionalMediaUnderstand invalid name
      r(198);

      Comment


      • #4
        To start with, you should tell whomever prepared the Stata dataset that they limit the variable names to 32 characters in the future.

        Secondly, I was able to devise a technique to work around this problem.
        Code:
        . describe, fullnames
        
        Contains data from /Users/lisowskiw/Downloads/long.dta
          obs:             1                          
         vars:             2                          27 Apr 2020 16:33
        ------------------------------------------------------------------------------------------------
                      storage   display    value
        variable name   type    format     label      variable label
        ------------------------------------------------------------------------------------------------
        id              float   %9.0g                 
        MessageTraditionalMediaUnderstand@���
                        float   %9.0g                 
        ------------------------------------------------------------------------------------------------
        Sorted by: 
             Note: Dataset has changed since last saved.
        
        . mata: st_varrename(2,"something_else")
        
        . describe, fullnames
        
        Contains data from /Users/lisowskiw/Downloads/long.dta
          obs:             1                          
         vars:             2                          27 Apr 2020 16:33
        ------------------------------------------------------------------------------------------------
                      storage   display    value
        variable name   type    format     label      variable label
        ------------------------------------------------------------------------------------------------
        id              float   %9.0g                 
        something_else  float   %9.0g                 
        ------------------------------------------------------------------------------------------------
        Sorted by: 
             Note: Dataset has changed since last saved.
        The first argument to st_varrename() tells it to rename the second variable in the dataset - in this case, the one with the long name.

        Comment


        • #5
          Thanks so much William. This is really clever. Is it possible to rename several variables at the same time using this technique? I tried:
          Code:
           
            mata: st_varrename((2,5), "something_else" "something_else1")
          but it didn't work.

          Comment


          • #6
            The output of help st_varrename() suggests not.

            Comment


            • #7
              Well, that is easy to program both in Stata and Mata. Here is a draft for Stata

              Code:
              local varindex 2 5
              local varnames "somehting_else" "something_else1"
              
              local i 0
              foreach idx of local varindex {
                  local name : word `++i' of `varnames'
                  mata : st_varrename(`idx', "`name'")
              }
              If you literally wanted the new names to be a common name with a running number added, then

              Code:
              local varindex 2 5
              
              local i 0
              foreach idx of local varindex {
                  mata : st_varrename(`idx', "something_else`++i'")
              }
              btw. StataCorp had a non-documented renamevarno that could be used if it is still around in Stata 16

              Code:
              local varindex 2 5
              
              local i 0
              foreach idx of local varindex {
                  varrenameno `idx'  something_else`++i'
              }
              You could shift the original name into the variable label or characteristics if you wanted to keep them. You did no ask for this, so I will stop here.

              Best
              Daniel

              Comment


              • #8
                Thanks a lot Daniel! This is very helpful.

                Comment

                Working...
                X