Search code examples
stata

invalid syntax and program error


forvalue n=1/18 {
    if f_3_`n'_==1 {
        local i= `0'
        local y=`i'+1 
            gen ownagri_`y' = f123a_`y'_
            replace ownagri_`y' = . if f_2_sel_`n' ==1 
        local i = `i'+1
    }
    else if f_3_`n'_==2 {
        local i= `0'
        local y=`i'+1 
            gen agri_`y' = f126_a1_`n'_
            replace agri_`y' = .if f_2_sel_`n' ==1 
        local i = `i'+1
    }
    else if f_3_`n'_==3 {
        local i= `0'
        local y=`i'+1 
            gen nonagri_`y' = f126_a1_`n'_
            replace nonagri_`y' = . if f_2_sel_`n' ==1 
        local i = `i'+1
    }
    else if f_3_`n'_==4 {
        local i=`0'
        local y=`i'+1 {
            gen nonagriself_`y' = f128_`n'_
            replace nonagriself_`y' = . if f_2_sel_`n' ==1 
        local i = `i'+1
    }
    else if f_3_`n'_==5 {
        local i=`0'
        local y=`i'+1 
            gen military_`y' = . if f_2_sel_`n' ==1  
        local i = `i'+1
    }}
}

Stata says my command contains invalid syntax and there is program error: code follows on the same line as close brace.

EDIT:

forvalue n = 1/18 {
    if f_3_`n'_==1 {
        local y1 = 1 
        gen ownagri_`y1' = f123a_`y1'_ 
        replace ownagri_`y' = . if f_2_sel_`n'_ ==1 
        local y1 = `y1'+1
    }
    else if f_3_`n'_==2 {
        local y2 = 1 
        gen agri_`y2' = f126_a1_`y2'_ 
        replace agri_`y2' = . if f_2_sel_`n'_ ==1 
        local y2 = `y2'+1
    }
    else if f_3_`n'_==3 {
        local y3 = 1
        gen nonagri_`y3' = f126_a1_`y3'_ 
        replace nonagri_`y3' = . if f_2_sel_`n'_ ==1 
        local y3 = `y3'+1
    }
    else if f_3_`n'_==4 {
        local y4 = 1
        gen nonagriself_`y4' = f128_`y4'_ 
        replace nonagriself_`y4' = . if f_2_sel_`n'_ ==1 
        local y4 = `y4'+1
    }
    else if f_3_`n'_==6 {
        local y5 = 1
        gen military_`y5' = f129a_`y5'_
        replace military_`y5' = . if f_2_sel_`n'_ ==1 ,modify
        local y5 = `y5'+1
    }
}

I modified the code and the program seems to work, but the results generated seem to be incomplete. The result shows as follow:

(20,070 missing values generated)
(2,194 real changes made, 2,194 to missing)
(19,229 missing values generated)
(1,129 real changes made, 1,129 to missing)

Why?


Solution

  • Specific comments already made:

    a. The }} on the next to last line should be } (William Lisowski)

    b. Lines like

    if f_3_`n'_==1 
    

    are evaluated as if

    if f_3_`n'_[1] ==1 
    

    which is usually not what is wanted. See this FAQ for much more. But this is not a syntax error.

    New specific comment:

    c. The line

    local y=`i'+1 {
    

    has a spurious { that should be removed.

    General comments:

    A. Throwing a large chunk of code at us without context is poor question style. You're new to this, which is fine, but equally there is accessible advice for you to follow e.g. on good examples.

    B. There is no context here on what you are trying to do and no data example. The Statalist advice on how to present data examples in its FAQ carries over to other sites with easy small modifications (e.g. the advice there on delimiters [CODE] and {/CODE] is irrelevant here).

    C. There is repeated code in every branch which can be moved, producing this:

    local i = `0'
    local y = `i'+1 
    
    forvalue n = 1/18 {
        if f_3_`n'_==1 {
            gen ownagri_`y' = f123a_`y'_
            replace ownagri_`y' = . if f_2_sel_`n' ==1 
        }
        else if f_3_`n'_==2 {
            gen agri_`y' = f126_a1_`n'_
            replace agri_`y' = .if f_2_sel_`n' ==1 
        }
        else if f_3_`n'_==3 {
            gen nonagri_`y' = f126_a1_`n'_
            replace nonagri_`y' = . if f_2_sel_`n' ==1 
        }
        else if f_3_`n'_==4 {
            gen nonagriself_`y' = f128_`n'_
            replace nonagriself_`y' = . if f_2_sel_`n' ==1 
        }
        else if f_3_`n'_==5 {
            gen military_`y' = . if f_2_sel_`n' ==1  
        }
    }
    
    local i = `i'+1
    

    Whether this code is what you want we cannot say, but it looks legal, and it's shorter than your original.