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.
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.
Comment