Announcement

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

  • Adding suffix to all variables after a given variable

    I am running the same code on different datasets. They all contain four initial variables: year, country, object, period, and then a list of city codes. Each file contains a different number of city codes. I would like to add a suffix to each city code, but want to have code that is general enough that it could apply to any of my files. The ideal scenario is one where all variables after the variable 'period' would contain a suffix.

    How can I specify that all variables, after a given variable (in this case 'period'), should be renamed to contain the suffix "c_"?

    Thanks




  • #2
    Perhaps it might easier be to rename all variables and then change back the first four:

    Code:
    rename * c_*
    rename c_year year
    rename c_country country
    rename c_object object
    rename c_period period

    Comment


    • #3
      I would not rely on the ordering of variables. If the four names are always the same, code

      Code:
      ds year country object period , not
      rename (`r(varlist)') (c_=)
      I assume by suffix you actually mean prefix.

      Best
      Daniel

      Comment


      • #4
        Thank you Igor Paploski and daniel klein. Both ideas work well! (And yes, I meant prefix.)

        Comment

        Working...
        X