Announcement

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

  • Renaming variables through looping

    Hi,

    I am quite new to looping in Stata. While I could actually rename groups of variables through the rename command, I still would like to learn how to do it by looping.

    n the codes below, I am trying to rename the variables in "food" to the actual name of the crops. However, my codes are not working. Hoping that someone could point out to me where I got it wrong.

    local crop "cereal root fruit meat dairy fish cocoa nonalc foodnes alc tobacco"
    local food "i1t_11100 i1t_11200 i1t_11300 i1t_11400 i1t_11500 i1t_11600 i1t_11700 i1t_11800 i1t_11900 i1t_13000 i1t_14000"

    foreach x of varlist `food' {
    rename `X' t_`crop'
    }

  • #2
    Lovely:
    welcome to the list.
    What if the `X' in the second line of your code becomes lower-case?
    As an aside, please report what you mean by "not working" and post not only what you typed, but also what Stata gave you back (as per FAQ). Thanks.
    Kind regards,
    Carlo
    (Stata 19.0)

    Comment


    • #3
      You must loop over two lists at once. You loop over the old names, but offer all the new names as a group each time. That will fail first time round the loop, as your offer of one old name and several new names will just confuse rename.

      Consider this:

      Code:
      local crop "cereal root fruit meat dairy fish cocoa nonalc foodnes alc tobacco"
      local food "i1t_11100 i1t_11200 i1t_11300 i1t_11400 i1t_11500 i1t_11600 i1t_11700 i1t_11800 i1t_11900 i1t_13000 i1t_14000"
      
      local nvars : word count `crop'
      
      forval j = 1/`nvars' {
          local old : word `j' of `food'
          local new : word `j' of `crop'
          display "`old' `new'"
      }

      Results are

      Code:
      i1t_11100 cereal
      i1t_11200 root
      i1t_11300 fruit
      i1t_11400 meat
      i1t_11500 dairy
      i1t_11600 fish
      i1t_11700 cocoa
      i1t_11800 nonalc
      i1t_11900 foodnes
      i1t_13000 alc
      i1t_14000 tobacco
      In your case you want

      Code:
      rename `old' `new'
      More on such loops at http://www.stata-journal.com/sjpdf.h...iclenum=pr0009 (Section 3)

      Comment


      • #4
        Hi,

        Nick Cox is of course correct. As an addition, you may want to have a look at Stata's -rename group- functionality, that has been introduced with Stata 13 (or was it 12?). You don't even have to use a loop at all: just use
        Code:
        local crop "cereal root fruit meat dairy fish cocoa nonalc foodnes alc tobacco"
        local food "i1t_11100 i1t_11200 i1t_11300 i1t_11400 i1t_11500 i1t_11600 i1t_11700 i1t_11800 i1t_11900 i1t_13000 i1t_14000"
        rename (`food') (`crop')
        Regards
        Bela

        PS: For the sake of readability of your Stata syntax, please consider using CODE tags for future posts.

        Comment


        • #5
          Lovely (is that a real name? sounds plausible in e.g. Philippines) did flag awareness of rename groups in #1. But I agree with Daniel. I wouldn't loop for an example like this.

          Comment

          Working...
          X