I am struggling to understand how can I loop over three locals in Stata.
Indeed, I am working with hourly weather data at the neighbourhood level and I need to generate an indicator function (i.e. a dummy) that takes value 1 when three weather conditions are met.
I am attaching a sample of my dataset and a dofile of the code that I need to automatize through a loop.
Here is a full description of what you will find in the dofile and in the data:
DATA: Hourly weather data at the neighbourhood level (id):
id | value1_r00 | value15_r00 | value1_r99 | value15_r99 | value1_r98 | value15_r98 | value1_s00 | value15_s00 | value1_s99 | value15_s99 |
1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
2 | 0 | 0 | 0 | 0 | 76 | 0 | 0 | 0 | 0 | 0 |
3 | 0 | 0 | 24 | 0 | 0 | 78 | 0 | 0 | 0 | 0 |
4 | 0 | 0 | 10 | 0 | 0 | 13 | 0 | 0 | 0 | 0 |
5 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
6 | 0 | 0 | 13 | 11.5 | 0 | 52 | 0 | 0 | 0 | 0 |
7 | 0 | 0 | 1.5 | 0 | 0 | 21 | 0 | 0 | 0 | 0 |
8 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
9 | 0 | 0 | 0 | 295 | 5 | 5 | 0 | 0 | 0 | 8 |
10 | 0 | 0 | 0 | 0 | 0 | 173 | 0 | 0 | 0 | 0 |
*Variabels description:
*Rain
*value1_r00 -> value1 (value of the element for the first hour) r (of element rain) 00(in year 2000)
*value15_r00 -> value15 (value of the element for the 15th hour) r (of element rain) 00( in year 2000)
*Snow
*value1_s00 -> value1 (value of the element for the first hour) s (of element snow) 00(in year 2000)
*value15_s00 -> value15 (value of the element for the 15th hour) s (of element snow) 00( in year 2000)
*Tmax
*value1_tmax00 -> value1 (value of the element for the first hour) tmax (of element maximum temp) 00(in year 2000)
*value15_tmax00 -> value15 (value of the element for the 15th hour) s (of element maximum temp) 00( in year 2000)
*Aim - generate a dummy variable at the neighbourhood level (id) that takes value 1 if three conditions are met:
*The volumes of rain in the first hour exceed 25.4, OR the volumes of snow in the first hour exceed 2.54 OR the max temperature in the first hour is below 0.
*Code for the first hour in the year 2000
gen d_value1_00=0
replace d_value1_00=1 if value1_r00>=25.4 | value1_s00>=2.54 | value1_tmax00<=0
replace d_value1_00=. if value1_r00==. & value1_s00==. & value1_tmax00==.
*Code for the first hour in the year 1999
gen d_value1_99=0
replace d_value1_99=1 if value1_r99>=25.4 | value1_s99>=2.54 | value1_tmax99<=0
replace d_value1_99=. if value1_r99==. & value1_s99==. & value1_tmax99==.
*Code for the first hour in the year 1998
gen d_value1_98=0
replace d_value1_98=1 if value1_r98>=25.4 | value1_s98>=2.54 | value1_tmax99<=0
replace d_value1_98=. if value1_r98==. & value1_s98==. & value1_tmax99==.
*Code for the 15th hour in the year 2000
gen d_value15_00=0
replace d_value15_00=1 if value15_r00>=25.4 | value15_s00>=2.54 | value15_tmax00<=0
replace d_value15_00=. if value15_r00==. & value15_s00==. & value15_tmax00==.
*Code for the 15th hour in the year 1999
gen d_value15_99=0
replace d_value15_99=1 if value15_r99>=25.4 | value15_s99>=2.54 | value15_tmax99<=0
replace d_value15_99=. if value15_r99==. & value15_s99==. & value15_tmax99==.
*Code for the 15th hour in the year 1998
gen d_value15_98=0
replace d_value15_98=1 if value15_r98>=25.4 | value15_s98>=2.54 | value15_tmax99<=0
replace d_value15_98=. if value15_r98==. & value15_s98==. & value15_tmax99==.
*Trying to automatize this through a loop: (obviously wrong, need hints!)
local rain "value1_r00 value15_r00 value1_r99 value15_r99 value1_r98 value15_r98"
local snow "value1_s00 value15_s00 value1_s99 value15_s99 value1_s98 value15_s98"
local tmax "value1_tmax00 value15_tmax00 value1_tmax99 value15_tmax99 value1_tmax98 value15_tmax98"
foreach var in `rain'{
gen d_`rain'=0
replace d_`rain'=1 if `rain'>=25.4
foreach var in `snow'{
replace d_`rain'=1 if `snow'>=2.54
foreach var in `tmax' {
replace d_`rain'=1 if `tmax'<0
}
}
}
Thanks to anyone who would like to help!
Best,
Alessandra
.
0 Response to Looping over three locals.
Post a Comment