Hello everyone!
I require assistance with writing nested loops.
My data consists of the following in respect of US states.
state name, year, month of the year, product description code, and monthly tax rate for products.
There are:
45 states
6 years (2000-2005),
12 months for each of the year
15 products (code 100 to 114)
I do the following
(1) I keep one year (2000), one month (1), one product (100), and all states.
(2) I manually calculate the geographical contiguity weight of one state (Washington) for that year, month, and product. (All 45 states have different monthly tax rates for that product)
I want to know now to repeat procedures (1) and (2) for all 6 years, 12 months, and 15 products.
I am sharing my code. The nested loop is not working.
Use data_states, clear
forvalues year= 2000/2005 {
forvalues month= 1/12 {
forvalues product = 100/114 {
keep if `year’==2000
keep if `month’==1
keep if `product’ == 100
generate washington_contiguity= 0
replace washington_contiguity = 1 if state== “Idaho”
replace washington_contiguity = 1 if state== “Oregon”
egen numerator=total(meantax_nonthly * washington_contiguity)
egen denominator= total (washington_contiguity)
generate weight_ washington_contiguity= numerator/ denominator
}
}
}
Thank you
I require assistance with writing nested loops.
My data consists of the following in respect of US states.
state name, year, month of the year, product description code, and monthly tax rate for products.
There are:
45 states
6 years (2000-2005),
12 months for each of the year
15 products (code 100 to 114)
I do the following
(1) I keep one year (2000), one month (1), one product (100), and all states.
| year | month | state | product code | monthly rate |
| 2000 | 1 | Alabama | 100 | 20 |
| 2000 | 1 | Arizona | 100 | 12 |
| 2000 | 1 | Arkansas | 100 | 25 |
I want to know now to repeat procedures (1) and (2) for all 6 years, 12 months, and 15 products.
I am sharing my code. The nested loop is not working.
Use data_states, clear
forvalues year= 2000/2005 {
forvalues month= 1/12 {
forvalues product = 100/114 {
keep if `year’==2000
keep if `month’==1
keep if `product’ == 100
generate washington_contiguity= 0
replace washington_contiguity = 1 if state== “Idaho”
replace washington_contiguity = 1 if state== “Oregon”
egen numerator=total(meantax_nonthly * washington_contiguity)
egen denominator= total (washington_contiguity)
generate weight_ washington_contiguity= numerator/ denominator
}
}
}
Thank you
