I´ve encountered a wierd situation today: failing to define a new scalar. Below is my code:
Code:
forvalues i = 2002(3)2014 { foreach k in real_estate house_debt other_debt ownership { forvalues j = 1/4 { *Create the sum of replicate weights cd $datainput/`i' use databol1, clear sum facine3 if percriq == `j' scalar weight_g`j' = r(sum) *Using combined imputation dataset to calculate mean of var of interest for each wealth group use comb_imp, clear mi import flong, m(mdataset) id(h_`i') mi estimate, esampvaryok post: reg `k' [pweight=facine3] if percriq == `j' mat b = e(b) scalar `k'_g`j' = b[1,1] *For ownership, our goal is to calculate ownership rate *For other variables, calculate the weighted sum if `k' == ownership { scalar w_`k'_g`j' = `k'_g`j' else { scalar w_`k'_g`j' = weight_g`j' * `k'_g`j' } } *Copy and paste the result, which is a scalar, to a new dataset cd $datatable\by_group use microagg_group, clear replace `k' = w_`k'_g`j' if year == `i' & group == `j' save microagg_group, replace } } }
Code:
replace `k' = w_`k'_g`j' if year == `i' & group == `j'
I checked scalar list by using scalar dir, it turns out scalar weight_g1 and scalar real_estate_g1 have been defined, which means that something wrong happened in the if clause. I then changed the structure to single command one:
Code:
if "`k'" == "ownership" scalar w_`k'_g`j' = `k'_g`j' else scalar w_`k'_g`j' = weight_g`j' * `k'_g`j'
What´s even more wierd, I succeeded at producing the result I want using the same code one hour ago (with trifling modification). The if structure was exactly the same in that code, and the names for scalars are the same as well.
Could someone provide some explanations? I´d appreciate your help!
0 Response to Failure to define a new scalar in a if clause
Post a Comment