Announcement

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

  • How to write a loop code for this following case as detailed in the post?

    I want to rename variables:

    rename f120_0_s1 f120_s1_0_
    rename f120_1_s1 f120_s1_1_
    rename f120_2_s1 f120_s1_2_
    rename f120_3_s1 f120_s1_3_
    rename f120_4_s1 f120_s1_4_
    rename f120_5_s1 f120_s1_5_
    rename f120_6_s1 f120_s1_6_
    rename f120_7_s1 f120_s1_7_
    rename f120_8_s1 f120_s1_8_
    rename f120_9_s1 f120_s1_9_
    rename f120_10_s1 f120_s1_10_
    rename f120_11_s1 f120_s1_11_
    rename f120_12_s1 f120_s1_12_
    rename f120_13_s1 f120_s1_13_
    rename f120_14_s1 f120_s1_14_
    rename f120_15_s1 f120_s1_15_

    rename f120_0_s2 f120_s2_0_
    rename f120_1_s2 f120_s2_1_
    rename f120_2_s2 f120_s2_2_
    rename f120_3_s2 f120_s2_3_
    rename f120_4_s2 f120_s2_4_
    rename f120_5_s2 f120_s2_5_
    rename f120_6_s2 f120_s2_6_
    rename f120_7_s2 f120_s2_7_
    rename f120_8_s2 f120_s2_8_
    rename f120_9_s2 f120_s2_9_
    rename f120_10_s2 f120_s2_10_
    rename f120_11_s2 f120_s2_11_
    rename f120_12_s2 f120_s2_12_
    rename f120_13_s2 f120_s2_13_
    rename f120_14_s2 f120_s2_14_
    rename f120_15_s2 f120_s2_15_




    till s17

    How to write loop code for this or any other parsimonious ways? I tried the following, but obviously there is something wrong:

    forvalues i = 0/15 j = 1/17 {
    rename f120_`i’_s`j’ f120_s`j’_`i’_
    }

    Many thanks!

  • #2
    Try

    Code:
    forvalues i = 0/15 {
    ren f120_`i'_s1 f120_s1_`i'_
    ren f120_`i'_s2 f120_s2_`i'_
    }

    Comment


    • #3
      Many thanks! But is it possible to be more parsimonious since I also need to write the loop from s1 to s17 at the same time?
      forvalues i = 0/15 { ren f120_`i'_s1 f120_s1_`i'_ ren f120_`i'_s2 f120_s2_`i'_ ren f120_`i'_s3 f120_s3_`i'_ ren f120_`i'_s4 f120_s4_`i'_ ren f120_`i'_s5 f120_s5_`i'_ ren f120_`i'_s6 f120_s6_`i'_ ren f120_`i'_s7 f120_s7_`i'_ ren f120_`i'_s8 f120_s8_`i'_ ren f120_`i'_s9 f120_s9_`i'_ ren f120_`i'_s10 f120_s10_`i'_ ren f120_`i'_s11 f120_s11_`i'_ ren f120_`i'_s12 f120_s12_`i'_ ren f120_`i'_s13 f120_s13_`i'_ ren f120_`i'_s14 f120_s14_`i'_ ren f120_`i'_s15 f120_s15_`i'_ ren f120_`i'_s16 f120_s16_`i'_ ren f120_`i'_s17 f120_s17_`i'_ }

      Comment


      • #4
        Many thanks! But is it possible to be more parsimonious since I also need to write the loop from s1 to s17 at the same time?
        forvalues i = 0/15 {
        ren f120_`i'_s1 f120_s1_`i'_
        ren f120_`i'_s2 f120_s2_`i'_
        ren f120_`i'_s3 f120_s3_`i'_
        ren f120_`i'_s4 f120_s4_`i'_
        ren f120_`i'_s5 f120_s5_`i'_
        ren f120_`i'_s6 f120_s6_`i'_
        ren f120_`i'_s7 f120_s7_`i'_
        ren f120_`i'_s8 f120_s8_`i'_
        ren f120_`i'_s9 f120_s9_`i'_
        ren f120_`i'_s10 f120_s10_`i'_
        ren f120_`i'_s11 f120_s11_`i'_
        ren f120_`i'_s12 f120_s12_`i'_
        ren f120_`i'_s13 f120_s13_`i'_
        ren f120_`i'_s14 f120_s14_`i'_
        ren f120_`i'_s15 f120_s15_`i'_
        ren f120_`i'_s16 f120_s16_`i'_
        ren f120_`i'_s17 f120_s17_`i'_ }

        Comment


        • #5
          Maybe like this:

          Code:
          forvalues i = 0/15 {
          forvalues j = 1/17 {
          ren f120_`i'_s`j' f120_s`j'_`i'_
          }
          }

          Comment


          • #6
            Many thanks! Stata says it is invalid syntax

            . do "/var/folders/h8/rkck_l9549327p28z_85fd640000gn/T//SD00296.000000"

            . forvalues i = 0/13,15 {
            2. forvalues j = 1/17 {
            3. ren f120_`i'_s`j' f120_s`j'_`i'_
            4. }
            5. }
            invalid syntax
            r(198);

            end of do-file

            Comment


            • #7
              Loops are not necessary.

              The output of help rename group (and the associated documentation in the Stata Data Management Reference Manual PDF linked to from the top of the help output) is very dense reading, so I'm not going to explain how the following complex rename command was constructed. Instead, I'll just show you the before-and-after with a small subset of your variables.
              Code:
              . describe, simple
              f120_0_s1    f120_14_s1   f120_1_s2    f120_15_s2   f120_2_s17
              f120_1_s1    f120_15_s1   f120_2_s2    f120_0_s17   f120_14_s17
              f120_2_s1    f120_0_s2    f120_14_s2   f120_1_s17   f120_15_s17
              
              . rename (f120_#_s#) (f120_s#[2]_#[1]_)
              
              . describe, simple
              f120_s1_0_    f120_s1_14_   f120_s2_1_    f120_s2_15_   f120_s17_2_
              f120_s1_1_    f120_s1_15_   f120_s2_2_    f120_s17_0_   f120_s17_14_
              f120_s1_2_    f120_s2_0_    f120_s2_14_   f120_s17_1_   f120_s17_15_

              Comment


              • #8
                Thank you very much! This code works very well!

                Comment


                • #9
                  Would you mind explaining this code to me: What are the () and [] for? I should have asked you this question earlier.

                  Comment


                  • #10
                    Have you reviewed the output of help rename group where this is documented? Or better yet, click through to the PDF documentation and read it carefully?

                    Those sources—which I referenced in the post where I used the rename command—explain rename group much better than I can. This version of the rename command is powerful, and a few sentences in a Statalist post won't help you understand the command, and by reading the documentation you'll understand all that the command can accomplish, not just this one little corner of its capability.

                    Comment


                    • #11
                      Really appreciate your help!

                      Comment

                      Working...
                      X