Hello community,
I have the following set-up:
An indicator (test_1) that shows whether some variables (ent_elect_1_1-ent_elect_1_4) have a "problem" in this observation. The "problem" is that the number of non-missing values in these variables (ent_elect_count_1) is larger than the value of another variable (enterp_nr_new_1). Now, I would like to put those variables to missing that are creating these problems. Namely, if test_1==1 because enterp_nr_new_1=2 & ent_elect_count_1=3, I would like ent_elect_1_3 to be replaced by "". Note that the variables ent_elect_1_1 to ent_elect_1_4 are always filled from "left to right", meaning if ent_elect_1_2 is missing, ent_elect_1_3 and ent_elect_1_4 will be missing, too.
The problem is a bit larger because I have this problem not only for 1 set-up but for 20, resulting in variables test_1 to test_20 and the number of ent_elect_i_j variables is with differing j depending on i (always below 11). My approach included some if conditons, for and while loops but it does not work (does not change the dataset).
Looking forward to some help.
Kind regards
Nina
I have the following set-up:
An indicator (test_1) that shows whether some variables (ent_elect_1_1-ent_elect_1_4) have a "problem" in this observation. The "problem" is that the number of non-missing values in these variables (ent_elect_count_1) is larger than the value of another variable (enterp_nr_new_1). Now, I would like to put those variables to missing that are creating these problems. Namely, if test_1==1 because enterp_nr_new_1=2 & ent_elect_count_1=3, I would like ent_elect_1_3 to be replaced by "". Note that the variables ent_elect_1_1 to ent_elect_1_4 are always filled from "left to right", meaning if ent_elect_1_2 is missing, ent_elect_1_3 and ent_elect_1_4 will be missing, too.
Code:
* Example generated by -dataex-. For more info, type help dataex clear input float(test_1 ent_elect_count_1 enterp_nr_new_1) str3 ent_elect_new_1_1 str1(ent_elect_new_1_2 ent_elect_new_1_3) str3 ent_elect_new_1_4 1 4 3 5 5 5 -88 0 0 . "" "" "" "" 0 0 . "" "" "" "" 0 0 . "" "" "" "" 0 0 . "" "" "" "" 0 0 . "" "" "" "" 0 0 . "" "" "" "" 0 1 1 "0" "" "" "" end label values enterp_nr_new_1 enterp_nr_1
Code:
forvalues i=1(1)20{ if test_`i'==1{ forvalues j=1(1)11{ if enterp_nr_new_`i'==' `j'{ local n=`j'+1 while `n' < 12{ capture confirm variable ent_elect_new_`i'_`n' if !_rc { replace ent_elect_new_`i'_`n'="" local n= `n'+1 } } } } } }
Kind regards
Nina
Comment