Announcement

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

  • How to rename all variables starting by the same string?

    Hi,

    I have +50 variables starting by "close". I would like to remove it systematically from the variable name. How can I detect these variables (some do not need that change) and proceed with the edit? Thanks!
    Example: "closepredict" to be renamed "predict"


  • #2
    Code:
    rename close* *

    Comment


    • #3
      Removing a prefix from a bunch of variable names is an early example in

      Code:
      help rename group
      Example:

      Code:
       
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input float(openfrog opentoad closefrog closetoad)
      42 42 42 42
      end
      
      . rename close* *
      
      . d
      
      Contains data
       Observations:             1                  
          Variables:             4                  
      -----------------------------------------------------------------------------------------
      Variable      Storage   Display    Value
          name         type    format    label      Variable label
      -----------------------------------------------------------------------------------------
      openfrog        float   %9.0g                 
      opentoad        float   %9.0g                 
      frog            float   %9.0g                 
      toad            float   %9.0g                 
      -----------------------------------------------------------------------------------------

      Comment


      • #4
        Thank you so much Daniel and Nick!

        Comment


        • #5
          I actually have a list of variables starting by "close" in a global variable. I would like to keep using the former "close" variable name, which are now rename, with the global variable. How can I do that?

          for example:

          global close_variables close_a close_b close_c

          * [..] Missing something here

          *** I would like to keep using $close_variables as if:
          global close_variables _a _b _c

          Comment


          • #6
            Code:
            global remove_close : subinstr global close_variables "close_" "" , all
            That said, do not use global variables! Use local macros.

            Comment


            • #7
              That said, (please, and in your best interests) do not use the term global variable! https://www.stata.com/statalist/arch.../msg01258.html

              Some readers may judge this a typical piece of pedantry (I call it precision) on my part, and they are right. It's on a level with underlining that Stata is not spelled STATA, and so forth. But consider this. I saw a posting somewhere from someone who obviously hires interns and longer-term staff to write Stata code. It was anonymous but utterly credible. The post ran like this: If I see an application using the spelling STATA, that is an immediate reject. It is not so much that it deeply matters how you spell the name; it is more that someone who has not picked up by now that the spelling is Stata hasn't been using it long enough for their experience and expertise to be serious. Also, they are signalling that they are people who don't pay much attention to detail, and for coding, that really, really matters.


              Comment

              Working...
              X