Hi all,

I have a do-file (Testing ceqef.do, attached) which I'm using to test an ado-file (ceqef.ado, attached). For the sake of simplicity, I copy the relevant part of the ado-file:

Code:
                    if (wordcount("`tax_`rw'_`cc''")>0 | wordcount("`ben_`rw'_`cc''")>0){ ;  //  #1
                            *impact effectiveness;
                            /*if wordcount("`benef'")>0{;
                                ceqbenstar [w=`w'], endinc(``cc'') ben(`benef');
                            };
                            if wordcount("`taxesef'")>0{;
                                ceqtaxstar [w=`w'], endinc(``cc'') taxes(`taxesef');
                            };*/
                            if (wordcount("`tax_`rw'_`cc''")>0 & wordcount("`ben_`rw'_`cc''")>0) {; //  #2
                                tempvar ystar;
                                gen double `ystar'=``rw'';
                                *set trace on ;
                                ceqtaxstar `pw', startinc(``rw'') taxes(`taxesef');    
                                *set trace off ;
                                local twarn = 0 ; 
                                if r(t_gr) == 1{ ;
                                    nois `dit'  "Sum of `tname_`rw'_`cc'' exceed ``rw'', so impact effectiveness indicator not produced from ``rw'' to ``cc''" ;
                                    local warning `warning'  "Sum of `tname_`rw'_`cc'' exceed ``rw'', so impact effectiveness indicator not produced from ``rw'' to ``cc''" ;
                                    local twarn = r(t_gr) ; 
                                } ;
                                else if r(t_0) == 1{ ;
                                    nois `dit'  "Sum of `tname_`rw'_`cc'' is 0, so impact effectiveness indicator not produced from ``rw'' to ``cc''" ;
                                    local warning `warning'  "Sum of `tname_`rw'_`cc'' exceed ``rw'', so impact effectiveness indicator not produced from ``rw'' to ``cc''" ;
                                    local twarn = r(t_0) ; 
                                } ;
                            
                                ceqbenstar `pw', startinc(``rw'') ben(`benef');
                                local bwarn = 0 ;
                                if r(b_gr) ==1 { ;
                                    nois `dit' "Sum of `bname_`rw'_`cc'' exceed ``rw'', so impact effectiveness indicator for ``rw'' to ``cc'' excludes benefits or is not produced" ;
                                    local warning `warning' "Sum of `bname_`rw'_`cc'' exceed ``rw'', so impact effectiveness indicator for ``rw'' to ``cc'' excludes benefits or is not produced" ;
                                    local bwarn = r(b_gr) ;
                                } ;
                                else if r(b_0) ==1 { ;
                                    nois `dit' "Sum of `bname_`rw'_`cc'' exceed ``rw'', so impact effectiveness indicator for ``rw'' to ``cc'' excludes benefits or is not produced" ;
                                    local warning `warning' "Sum of `bname_`rw'_`cc'' exceed ``rw'', so impact effectiveness indicator for ``rw'' to ``cc'' excludes benefits or is not produced" ;
                                    local bwarn = r(b_0) ;
                                } ;
                                
                                if `bwarn' == 0 & `twarn' == 0 { ;
                                    replace `ystar'=____ybenstar if ____id_benstar==1 & ____id_taxstar!=1;
                                    replace `ystar'=____ytaxstar if ____id_taxstar==1 & ____id_benstar!=1;
                                    tempvar temptax;
                                    gen double    `temptax'=``rw''-    ____ytaxstar if ____id_benstar==1 & ____id_taxstar==1;            
                                    tempvar tempben;
                                    gen double    `tempben'=    ____ybenstar - ``rw'' if ____id_benstar==1 & ____id_taxstar==1;
                                    replace `ystar'=``rw'' - `temptax' +`tempben' if ____id_benstar==1 & ____id_taxstar==1;            
                                    cap drop ____ytaxstar ____ybenstar ____id_benstar ____id_taxstar ;
                                    cap drop `temptax' `tempben';
                                };
                                else  { ;
                                    local bwarn = 1 ;
                                    local twarn = 1 ;
                                };
                            };
                            if (wordcount("`tax_`rw'_`cc''")>0 & wordcount("`ben_`rw'_`cc''")==0) {;  // #3
                                *set trace on;
                                ceqtaxstar `pw' , startinc(``rw'') taxes(`taxesef') ;
                                *set trace off;
                                local twarn = 0 ;
                                if  r(t_gr) ==1 { ;
                                    nois `dit' "Sum of `tname_`rw'_`cc'' exceed ``rw'', so impact effectiveness indicator not produced from ``rw'' to ``cc''" ;
                                    local warning `warning' "Sum of `tname_`rw'_`cc'' exceed ``rw'', so impact effectiveness indicator not produced from ``rw'' to ``cc''" ;                                
                                    local twarn = r(t_gr) ; 
                                } ;
                                else if  r(t_0) ==1 { ;
                                    nois `dit' "Sum of `tname_`rw'_`cc'' is 0, so impact effectiveness indicator not produced from ``rw'' to ``cc''" ;
                                    local warning `warning' "Sum of `tname_`rw'_`cc'' is 0, so impact effectiveness indicator not produced from ``rw'' to ``cc''" ;                                
                                    local twarn = r(t_0) ; 
                                } ;
                                else !(r(t_0) == 1 | r(t_gr) == 1) {;
                                    tempvar ystar;
                                    gen double `ystar'=____ytaxstar;
                                    cap drop ____ytaxstar ____ybenstar ____id_benstar ____id_taxstar;
                                };
                            };
                            if (wordcount("`tax_`rw'_`cc''")==0 & wordcount("`ben_`rw'_`cc''")>0) {;  // #4
                                ceqbenstar `pw', startinc(``rw'') ben(`benef');        
                                local bwarn = 0 ;
                                if r(b_gr) == 1 { ;
                                    nois `dit' "Sum of `bname_`rw'_`cc'' is 0, so impact effectiveness indicator not produced from ``rw'' to ``cc''" ;
                                    local warning `warning' "Sum of `bname_`rw'_`cc'' exceed ``rw'', so impact effectiveness indicator not produced from ``rw'' to ``cc''" ;
                                    local bwarn = r(b_gr) ;
                                } ;
                                else if r(b_0) == 1 { ;
                                    nois `dit' "Sum of `bname_`rw'_`cc'' is 0, so impact effectiveness indicator not produced from ``rw'' to ``cc''" ;
                                    local warning `warning' "Sum of `bname_`rw'_`cc'' exceed ``rw'', so impact effectiveness indicator not produced from ``rw'' to ``cc''" ;
                                    local bwarn = r(b_0) ;
                                } ;
                                if !(r(b_0) == 1 | r(b_gr) == 1) {;            
                                    tempvar ystar;
                                    gen double `ystar'=____ybenstar;
                                    cap drop ____ytaxstar ____ybenstar ____id_benstar ____id_taxstar;
                                };
                        };
                    
                           
                            if (!( "`bwarn'" == "1" & "`twarn'" == "1" )) { ; // #5
                                covconc ``cc'' `pw'; //gini of column income;
                                local g1_`cc'=r(gini);
                                di "`rw' ``rw''";
                                covconc ``rw'' `pw'; //gini of row income;
                                local g2_`rw'=r(gini);
                                covconc `ystar' `pw'; //gini of star income;
                                local g_star=r(gini);
                                local imef=(`g2_`rw''-`g1_`cc'')/(`g2_`rw''-`g_star');
                                matrix `rw'_ef[1,`_`cc'']=`imef';
                            };
Where I've numbered what I think are the 5 most important -if- statements and the #1 continues after #5 is closed. My problem is that when running the do-file, there are certain cases in which `tax_`rw'_`cc'' and `ben_`rw'_`cc'' don't exist and therefore -if- #1 should be false and I would expect Stata to skip everything inside the brackets corresponding to that -if-. However, even though I've confirmed that, in fact, Stata evaluates -if- #1 as false and skips #2 and #3, it runs -if- #4 and #5 (and since there are variables that are not created when the -if- #1 is false, Stata runs into an error and stops running). You can see this in the relevant part of the log-file that I now copy:


- if (wordcount("`tax_`rw'_`cc''")>0 | wordcount("`ben_`rw'_`cc''")>0){ // #1
= if (wordcount("")>0 | wordcount("")>0){ // #1
if (wordcount("`tax_`rw'_`cc''")>0 & wordcount("`ben_`rw'_`cc''")>0) { // #2
tempvar ystar
gen double `ystar'=``rw''
ceqtaxstar `pw', startinc(``rw'') taxes(`taxesef')
local twarn = 0
if r(t_gr) == 1{
nois `dit' "Sum of `tname_`rw'_`cc'' exceed ``rw'', so impact effectiveness indicator not produced from
> ``rw'' to ``cc''"
local warning `warning' "Sum of `tname_`rw'_`cc'' exceed ``rw'', so impact effectiveness indicator not
> produced from ``rw'' to ``cc''"
local twarn = r(t_gr)
}
else if r(t_0) == 1{
nois `dit' "Sum of `tname_`rw'_`cc'' is 0, so impact effectiveness indicator not produced from ``rw'' t
> o ``cc''"
local warning `warning' "Sum of `tname_`rw'_`cc'' exceed ``rw'', so impact effectiveness indicator not
> produced from ``rw'' to ``cc''"
local twarn = r(t_0)
}
ceqbenstar `pw', startinc(``rw'') ben(`benef')
local bwarn = 0
if r(b_gr) ==1 {
nois `dit' "Sum of `bname_`rw'_`cc'' exceed ``rw'', so impact effectiveness indicator for ``rw'' to ``c
> c'' excludes benefits or is not produced"
local warning `warning' "Sum of `bname_`rw'_`cc'' exceed ``rw'', so impact effectiveness indicator for
> ``rw'' to ``cc'' excludes benefits or is not produced"
local bwarn = r(b_gr)
}
else if r(b_0) ==1 {
nois `dit' "Sum of `bname_`rw'_`cc'' exceed ``rw'', so impact effectiveness indicator for ``rw'' to ``c
> c'' excludes benefits or is not produced"
local warning `warning' "Sum of `bname_`rw'_`cc'' exceed ``rw'', so impact effectiveness indicator for
> ``rw'' to ``cc'' excludes benefits or is not produced"
local bwarn = r(b_0)
}
if `bwarn' == 0 & `twarn' == 0 {
replace `ystar'=____ybenstar if ____id_benstar==1 & ____id_taxstar!=1
replace `ystar'=____ytaxstar if ____id_taxstar==1 & ____id_benstar!=1
tempvar temptax
gen double `temptax'=``rw''- ____ytaxstar if ____id_benstar==1 & ____id_taxstar==1
tempvar tempben
gen double `tempben'= ____ybenstar - ``rw'' if ____id_benstar==1 & ____id_taxstar==1
replace `ystar'=``rw'' - `temptax' +`tempben' if ____id_benstar==1 & ____id_taxstar==1
cap drop ____ytaxstar ____ybenstar ____id_benstar ____id_taxstar
cap drop `temptax' `tempben'
}
else {
local bwarn = 1
local twarn = 1
}
}
if (wordcount("`tax_`rw'_`cc''")>0 & wordcount("`ben_`rw'_`cc''")==0) { // #3
ceqtaxstar `pw' , startinc(``rw'') taxes(`taxesef')
local twarn = 0
if r(t_gr) ==1 {
nois `dit' "Sum of `tname_`rw'_`cc'' exceed ``rw'', so impact effectiveness indicator not produced from
> ``rw'' to ``cc''"
local warning `warning' "Sum of `tname_`rw'_`cc'' exceed ``rw'', so impact effectiveness indicator not
> produced from ``rw'' to ``cc''"
local twarn = r(t_gr)
}
else if r(t_0) ==1 {
nois `dit' "Sum of `tname_`rw'_`cc'' is 0, so impact effectiveness indicator not produced from ``rw'' t
> o ``cc''"
local warning `warning' "Sum of `tname_`rw'_`cc'' is 0, so impact effectiveness indicator not produced
> from ``rw'' to ``cc''"
local twarn = r(t_0)
}
else !(r(t_0) == 1 | r(t_gr) == 1) {
tempvar ystar
gen double `ystar'=____ytaxstar
cap drop ____ytaxstar ____ybenstar ____id_benstar ____id_taxstar
}
}
- if (wordcount("`tax_`rw'_`cc''")==0 & wordcount("`ben_`rw'_`cc''")>0) { // #4
= if (wordcount("")==0 & wordcount("")>0) {
ceqbenstar `pw', startinc(``rw'') ben(`benef')
local bwarn = 0
if r(b_gr) == 1 {
nois `dit' "Sum of `bname_`rw'_`cc'' is 0, so impact effectiveness indicator not produced from ``rw'' t
> o ``cc''"
local warning `warning' "Sum of `bname_`rw'_`cc'' exceed ``rw'', so impact effectiveness indicator not
> produced from ``rw'' to ``cc''"
local bwarn = r(b_gr)
}
else if r(b_0) == 1 {
nois `dit' "Sum of `bname_`rw'_`cc'' is 0, so impact effectiveness indicator not produced from ``rw'' t
> o ``cc''"
local warning `warning' "Sum of `bname_`rw'_`cc'' exceed ``rw'', so impact effectiveness indicator not
> produced from ``rw'' to ``cc''"
local bwarn = r(b_0)
}
if !(r(b_0) == 1 | r(b_gr) == 1) {
tempvar ystar
gen double `ystar'=____ybenstar
cap drop ____ytaxstar ____ybenstar ____id_benstar ____id_taxstar
}
}
- assert !( "`bwarn'" == "1" & "`twarn'" == "1" )
= assert !( "" == "1" & "" == "1" )
- pause: pause2
pause: : pause2
-> . q
execution resumes...
- if (!( "`bwarn'" == "1" & "`twarn'" == "1" )) { // #5
= if (!( "" == "1" & "" == "1" )) {
- covconc ``cc'' `pw'
= covconc OI [pw = __000001]
- local g1_`cc'=r(gini)
= local g1_mp=r(gini)
- di "`rw' ``rw''"
= di "m OI"
- covconc ``rw'' `pw'
= covconc OI [pw = __000001]
- local g2_`rw'=r(gini)
= local g2_m=r(gini)
- covconc `ystar' `pw'
= covconc [pw = __000001]
varlist required
local g_star=r(gini)
local imef=(`g2_`rw''-`g1_`cc'')/(`g2_`rw''-`g_star')
matrix `rw'_ef[1,`_`cc'']=`imef'
}


Since I set the trace on, you can clearly see which -if- statements run. I really don't understand why. I've checked multiple times if all the -if- statements were properly closed, and as far as I can tell, they are. Can you think of another reason why this -if- statement is not working as it should (others in the ado-file work perfectly fine)?

Many thanks in advance for your help!