*! version 1.1  repeated measures anova  gmh 5/7/98
*! syntax rmanova y_var subject_var within_var between_var1 between_var2 between_var3
program define rmanova
    version 4.0
    local varlist "req ex min(3) max(6)"
    local if "opt"
    local in "opt"
    local weight "aweight fweight"
    #delimit ;
    #delimit cr
    parse "`*'"
    parse "`varlist'", parse(" ")

    display "ANOVA for var `1' by subject `2'"

    if "`6'" ~= "" {anova  `1' `4' `5' `6' `2'*`4'*`5'*`6' [`weight' `exp'] `if' `in', noanova
	display "n=" _result(1) " df=" _result(3) " R2="_result(7)
	display "between effect: `4'"
    	test `4', error(`2'*`4'*`5'*`6')
	display "between effect: `5'"
    	test `5', error(`2'*`4'*`5'*`6')
	display "between effect: `6'"
    	test `6', error(`2'*`4'*`5'*`6')}


    else if "`5'" ~= "" {anova  `1' `4' `5'  `2'*`4'*`5' [`weight' `exp'] `if' `in', noanova
	display "n=" _result(1) " df=" _result(3) " R2="_result(7)
	display "between effect: `4'"
    	test `4', error(`2'*`4'*`5')
	display "between effect: `5'"
    	test `5', error(`2'*`4'*`5')}

    else if "`4'" ~= "" {anova  `1' `4' `2'*`4' [`weight' `exp'] `if' `in', noanova
	display "n=" _result(1) " df=" _result(3) " R2="_result(7)
	display "between effect: `4'"
    	test `4', error(`2'*`4')}

    else display "no between effects"


    display "within effect: `3'"
    anova `1' `2' `3' [`weight' `exp'] `if' `in', noanova
    test `3'

end