Announcement

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

  • Loop forv

    Hi guys,

    I try to wirte my first loop, but it does not work well. Hopefully, you may help.

    I have data of an experiment which obtains 32 rounds. I would like to rename the variables, e.g., Player.1.income => income1.

    Due to my research, I started with the follwing loop:

    forv num=1/32 {
    drop Player.`num'.income
    }
    But what is coming next? I only defined the "first part" and do not tell Stata to change it into income1.

    Im looking forward to hearing from you!
    Last edited by Vera Schmidt; 06 Sep 2023, 09:06.

  • #2
    Can you double check your data and make sure the variable name is really "Player.1.income"? In Stata period (.) is not allowed in variable names.

    Assuming there is no period, a possible

    Code:
    forvalues x = 1/32{
    rename Player`x'income income`x'
    }
    Last edited by Ken Chui; 06 Sep 2023, 09:39.

    Comment


    • #3
      Wow, yet so simple. Many thanks & works well!

      Comment


      • #4
        If a purpose of this is to practice writing loops, then the solution in #2 is optimal.

        But if the purpose is to write production code, even simpler is:
        Code:
        rename (Player*income) (income*)
        which involves no loop at all.

        Comment

        Working...
        X