Announcement

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

  • Adding prefix to many variable names to prep for reshape

    I have many variables that need to be reshaped from wide form to long form. Each is the name of a county and the values represent the population at different age groups. Before reshaping this data (from Counties in columns to columns in rows) I need to give them all an identical prefix. I would like to simply add "pop" to the front of all the County variable names. I can do this individually using:

    rename County1 popCounty1

    but this would take a very long time. I have tried several methods for doing this for a long list of variables without success. My most recent attempt included using the renvars command as follows:

    renvars CountyVariablesList prefix(pop)

    and was given the error "factor variables and time-series operators not allowed"

    I've also tried using rename as follows:

    rename CountyVariablesList pop=:

    but I think I'm interpreting the help guide wrong on this one. It says:


    rename whatever pre=fix: Adds prefix pre and suffix fix to all variables selected by whatever. & that "whatever is specified" -- > I'm not sure how to specify "whatever" other than listing the variables I want to update.


    Any help would be much appreciated, thanks!
    Last edited by Erin Burkett; 16 Sep 2015, 08:55.

  • #2
    With renvars (from the Stata Journal, as you are asked to explain), you certainly omitted the comma indicating an option:

    Code:
    renvars CountyVariablesList,  prefix(pop)
    but there seems to be no reason given in the rest of your post to give the literal text

    Code:
    CountyVariablesList
    With rename the example in the help

    Code:
    rename (status bp time) admit=
    seems closest to your problem. The colon is not a syntactic element here; it is just part of the punctuation used in the help.

    The following examples, which you can replicate, show that you can use wildcards and variable ranges when adding a prefix:

    Code:
    . sysuse auto, clear
    (1978 Automobile Data)
    
    . rename (m*) (FROG=)
    
    . ds
    FROGmake      rep78         weight        displacement
    price         headroom      length        gear_ratio
    FROGmpg       trunk         turn          foreign
    
    . rename (FROGmpg-turn) (TOAD=)
    
    . ds
    FROGmake      TOADrep78     TOADweight    displacement
    price         TOADheadroom  TOADlength    gear_ratio
    TOADFROGmpg   TOADtrunk     TOADturn      foreign
    In your case the solution might be as simple as

    Code:
     
    rename (County*) (pop=)
    but if not you need to tell us more.

    Comment


    • #3
      Hi Erin,

      I didn't try it out, for I'm not with "my Stata" now, but you may check this:

      Code:
      . rename County# popCounty#
      Hopefully that helps.

      Best,

      Marcos
      Best regards,

      Marcos

      Comment


      • #4
        Yes all I was missing is the comma, and after adding that using the renvars command from the Stata Journal worked just fine for what I needed, which was to add some stub to the front of all my County variables so they could be reshaped from wide to long. Some of the basics (like the comma always required for denoting options) are still not automatic for me. Thanks a lot for the help!

        Comment

        Working...
        X