Announcement

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

  • renaming variables dropping characters after integers and reshapping to long

    I'm using Stata 14 on a windows 10 OS.

    I have a quarterly data set with very long variables names in a wide dataset

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str13 Name str10(CtaRecCPDez2017emmoedaori CtaRecCPSet2017emmoedaori CtaRecCPJun2017emmoedaori FornecDez2017emmoedaorig) str9 FornecSet2017emmoedaorig str10 FornecJun2017emmoedaorig
    "524 Particip" "0" "0" "0" "0"   "0"   "0"
    "AES Elpa"     "0" "0" "0" "15"  "31"  "27"
    "Alef S/A"     "0" "0" "0" "0"   "0"   "0"
    "Alfa Holding" "0" "0" "0" "194" "146" "73"
    "Alfa Holding" "0" "0" "0" "194" "146" "73"
    end
    I want to drop all characters after the years, but the variable names (first part of characters) have different numbers of characters to keep. Is that a straightforward way to rename all variables at once?

    One more thing, if I keep CtaRecCPDez2017, FornecDez2017 (...) can I use the following code to reshape wide to long?

    Code:
    reshape long CtaRecCPDez2017 FornecDez2017, i(A) j(Year)

  • #2
    Code:
    *suppsoing all dates are in the 2000's:
    ren *20??* *20??
    
    *because there are exact copies of your data, and Stata cannot handle these in the reshape:
    duplicates drop
    
    ren * value*
    ren valueName Name
    reshape long value, i(Name) j(varname) string
    
    *get out the dates
    gen year = substr(varname,-4,.)
    replace varname = substr(varname,1,strlen(varname)-4)
    gen month = substr(varname,-3,.)
    replace varname = substr(varname,1,strlen(varname)-3)
    
    *probably followed by:
    reshape wide value, i(Name year month) j(varname) string
    ren value* *

    Comment


    • #3

      Then all you need is
      Code:
      ren *20??* *20??
      ren *19??* *19??

      Comment


      • #4
        Yes, of course. I didn´t pay attention to this part of your code. Sorry. That does not solve my problem.
        I have a lot of variables. I´d like to do it quickly.
        Is that a way to rename all variables, with few exceptions?
        Last edited by Felipe Damasceno; 05 Jun 2018, 12:21.

        Comment


        • #5
          The code in #3 will deal with as many variables as there are. Did you try using it?
          The * is a wildcard for any length of characters, whilts the ? is a wildcard for any individual character. So those two lines are good for your purpose

          Comment


          • #6
            wow. Thanks. I though it was just a generic thing.

            Comment

            Working...
            X