Announcement

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

  • Looping code in stata

    Hi everyone,

    I am trying to loop code in StataMP (14.0) so that it performs a set of actions on multiple variables.
    My dataset is made of triplets of variables, named in alphabetical order. Here I trimmed the dataset to only 2 triplets for simplicity:

    obs: 267
    vars: 6 6 Jul 2023 22:20
    size: 9,078
    -----------------------------------------------------------------------------------------------------------------------------------
    storage display value
    variable name type format label variable label
    -----------------------------------------------------------------------------------------------------------------------------------
    A double %10.0g A
    B double %10.0g B
    C byte %10.0g C
    D double %10.0g D
    E double %10.0g E
    F byte %10.0g F
    -----------------------------------------------------------------------------------------------------------------------------------

    Hi everyone,

    I am trying to loop code in StataMP (14.0) so that it performs a set of actions on multiple variables.
    My dataset is made of triplets of variables, named in alphabetical order. Here I trimmed the dataset to only 2 triplets for simplicity:

    obs: 267
    vars: 6 6 Jul 2023 22:20
    size: 9,078
    -----------------------------------------------------------------------------------------------------------------------------------
    storage display value
    variable name type format label variable label
    -----------------------------------------------------------------------------------------------------------------------------------
    A double %10.0g A
    B double %10.0g B
    C byte %10.0g C
    D double %10.0g D
    E double %10.0g E
    F byte %10.0g F
    -----------------------------------------------------------------------------------------------------------------------------------

    [CODE]
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input double(A B) byte C double(D E) byte F
    .52 99.95 34 .07 99.89 82
    .76 99.95 34 .21 99.89 82
    1 99.95 34 .36 99.95 82
    1.24 99.95 34 .51 100 82
    1.48 99.95 34 .65 100 82
    1.72 99.95 34 .8 100 82
    1.96 99.95 34 .95 100 82
    2.2 99.95 34 1.09 100 82
    2.44 99.95 34 1.24 100 82
    2.68 99.95 34 1.39 99.99 82
    2.92 99.88 34 1.53 99.89 82
    3 98.14 34 1.68 99.89 82
    2.98 95.77 34 1.83 99.89 82
    2.98 93.77 34 1.97 99.89 82
    2.98 91.82 34 2.12 99.89 82
    2.98 89.77 34 2.27 99.89 82
    2.98 87.77 34 2.41 99.89 82
    2.98 85.77 34 2.56 99.89 82
    2.98 83.77 34 2.71 99.89 82
    2.99 81.66 34 2.85 99.93 82
    2.98 79.73 34 3 100 82
    2.98 77.73 34 3.15 100 82
    ...


    I need to loop the following code, so that it first runs for the first triplet (A B C), then for the second (D E F), and so on, until the end of the dataset:
    replace C = C[1]
    gen A_rounded= round(A, 0.1)
    gen B_rounded = round((((round((B/100*C),1))/C)*100), 0.01)
    sort A B
    egen B_min = min(B_rounded), by(A_rounded)
    order A_rounded, before(C)
    order B_min, after(A_rounded)
    drop A B B_rounded

    However, I am unable to define the loop so that it runs on A B C, and then repeats the code for the next triplet, substituting the A B C variables with the corresponding variable in the next triplet. You can say that in the code, A should always refer to the first variable in the triplet, B to the second, and C to the third.

    Can anyone help with the loop, or offer suggestions?

    Many thanks.

    I need to loop the following code, so that it first runs for the first triplet (A B C), then for the second (D E F), and so on, until the end of the dataset:
    replace C = C[1]
    gen A_rounded= round(A, 0.1)
    gen B_rounded = round((((round((B/100*C),1))/C)*100), 0.01)
    sort A B
    egen B_min = min(B_rounded), by(A_rounded)
    order A_rounded, before(C)
    order B_min, after(A_rounded)
    drop A B B_rounded

    However, I am unable to define the loop so that it runs on A B C, and then repeats the code for the next triplet, substituting the A B C variables with the corresponding variable in the next triplet. You can say that in the code, A should always refer to the first variable in the triplet, B to the second, and C to the third.

    Can anyone help with the loop, or offer suggestions?

    Many thanks.
    Last edited by Eduardo Urgesi; 06 Jul 2023, 16:15.

  • #2
    Here's one way:
    Code:
    unab triplet_vars: A-F
    local n_vars: word count `triplet_vars'
    //  VERIFY NUMBER OF VARIABLES IS A MULTIPLE OF 3
    assert mod(`n_vars', 3) == 0
    
    forvalues i = 1(3)`=`n_vars'-2' {
        local first `:word `i' of `triplet_vars''
        local second `:word `=`i'+1' of `triplet_vars''
        local third `:word `=`i'+2' of `triplet_vars''
        display `"`first' `second' `third'"'
        replace `third' = `third'[1]
        gen `first'_rounded = round(`first', 0.1)
        gen `second'_rounded = round((((round((`second'/100*`third'),1))/`third')*100), 0.01)
        sort `first' `second'
        egen `second'_min = min(`second'_rounded), by(`first'_rounded)
        order `first'_rounded, before(`third')
        order `second'_min, after(`first'_rounded)
        drop `first' `second' `second'_rounded
    }

    Comment


    • #3
      Works perfectly! Many thanks Dr Schechter

      Comment

      Working...
      X