Announcement

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

  • Variable renames in a loop

    Hi to all,
    I have a list of variables: ita_a01_02, ita_a03, ita_b05_06 and ita_b07_08

    Does anyone know how to generate the following variables in a loop?
    ita_1 for ita_a01_02
    ita_2 for ita_a03
    ita_3 for ita_b05_06
    ita_4 for ita_b07_08

    Add to this that I would like to repeat it also with a loop on a similar list of variables (bel_a01_02 bel_a03 bel_b05_06 bel_b07_08), from which i would like to get bel_1, ..., bel_4.

    Thank you.
    Francesco

  • #2
    That's not in essence a loop problem as you can do in one line as

    Code:
    rename (ita_a01_02 ita_a03 ita_b05_06  ita_b07_08) (ita_#), addnumber
    or possibly as

    Code:
    rename (ita*) (ita_#), addnumber
    I dare say you could loop over

    foreach pre in ita bel {
    rename `pre'* `pre'_#, addnumber
    }

    More general solutions depend on what other variables you do or do not have (and so on whether wild cards match too much).

    But replacing two lines with a loop and three lines isn't obvious gain. Also true that you don't learn the technique that can be really helpful for larger problems except by learning it on small problems.

    Comment


    • #3
      Dear Nick it, the looo works perfectly. My example was on few variables, actually I have 45 x 77 variables (77 countries and 45 industries), so the gain, as your solution, is great!

      Comment

      Working...
      X