Hello,
I'm a newbie to stata. I'm doing a replication study for my school work, codes are provided and I just need to re-run the codes to get the same result as the author's - so obviously I don't know much about the codes either.
and.. I got "varlist not allowed" error message when I keyed in an "outreg" code.
Could anyone please advise on this?
The bold code below is where I got the error message from.

Thank you!

#delimit
clear
set mem 400m
set matsize 400
set more off

cd "/datasets/public"

do do/create_data_studycohort.do
do do/create_data_controlcohort.do


************************************************** *****************
************ TABLE 1 **********************************************
************************************************** *****************

*Panel A
use /datasets/class8priors, clear
gen obs=1
sort schoolid
gen newschool=1 if schoolid!=schoolid[_n-1]
egen Nschools=count(newschool)

* create dummies of interest
gen girl=1 if sex=="F"
replace girl=0 if sex=="M"
gen SDriskier=0
replace SDriskier=1 if _risk_partner==3
gen CondomPreventPreg=0
replace CondomPreventPreg=1 if _condom_preg<4
gen CondomPreventHIV=0
replace CondomPreventHIV=1 if _condom_hiv<4
gen CondomIneffPreg=0
replace CondomIneffPreg=1 if _condom_preg==4
gen CondomIneffHiv=0
replace CondomIneffHiv=1 if _condom_hiv==4
gen sexactive=0
replace sexactive=1 if _ever_sex==1
gen reportgirlswithSD=0
replace reportgirlswithSD=1 if index(_sugardaddies, "1")|index(_sugardaddies, "2")|index(_sugardaddies, "3")|index(_sugardaddies, "4")
replace reportgirlswithSD=1 if index(_sugardaddies, "5")|index(_sugardaddies, "6")|index(_sugardaddies, "7")|index(_sugardaddies, "8")|index(_sugardaddies, "9")
replace reportgirlswithSD=1 if index(_sugardaddies,"ALL")|index(_sugardaddies,"MA NY")|index(_sugardaddies,"FEW")
replace reportgirlswithSD=1 if index(_sugardaddies,"MANY")
gen pupilage=age
replace pupilage=2004-1989 if age==1909
replace pupilage=2004-1993 if age==1963
replace pupilage=2004-age if age>1982 & age!=.
replace pupilage=104-age if age>84&age<90
replace pupilage=19 if age==29
replace pupilage=14 if age==74
replace pupilage=. if pupilage<11
save /datasets/class8priors_clean, replace
collapse pupilage CondomPreventPreg CondomPreventHIV SDriskier sexactive reportgirlswithSD Nschools (sum)obs, by(girl)
outsheet using datasets/results/sumstatsclass8.xls,replace
use class8priors_clean, clear
collapse (sd) pupilage CondomPreventPreg CondomPreventHIV SDriskier sexactive reportgirlswithSD, by(girl)
outsheet using /datasets/results/sumstatsclass8sd.xls,replace
erase /datasets/class8priors_clean.dta


*Panel B
use /datasets/homesurvey,clear
***CREATE OUTCOMES OF INTEREST
**WANTED PREG**
gen fert=0
replace fert=1 if givenbirth==1|stillbirth==1|pregnantnow=="1"
replace fert=1 if pregnantnow=="99"|index(pregnantnow, "D")
gen wantedpreg=0 if fert==1
replace wantedpreg=1 if wantedpreg_22==1| wantedpreg_25==1
***NOT CONSENSUAL**
gen rape=0
replace rape=1 if index(comments, "RAPE")|index(typeofhelp_43, "RAPE")
**PAYMENTS**
destring cashbefore, force replace
replace cashbefore=0 if cashbefore==2|cashbefore==2
replace cashbefore=0 if cashbefore==.&support_42==2
replace cashbefore=0 if cashbefore==3
destring stillsupporting, force replace
replace stillsupporting=0 if stillsupporting==2
*BABY'S FATHER'S AGE
replace agefather=2005-agefather if agefather>1900&age<1991
gen agegap=agefather-age
replace agegap=. if agegap>39
gen gapabove5=0 if agegap!=.|yrsolder_27a<3
replace gapabove5=1 if agegap>5&agegap!=.
replace gapabove5=1 if yrsolder_27a==1|yrsolder_27b==1
gen gapabove10=0 if agegap!=.|yrsolder_27b<3
replace gapabove10=1 if agegap>10&agegap!=.
replace gapabove10=1 if yrsolder_27b==1
replace gapabove5=1 if gapabove5==.&gapabove10==0
replace gapabove10=0 if gapabove10==.&gapabove5==0
***MARRIED***
gen married=0 if relationship!=.
replace married=1 if relationship==1
gen married_u5=married if gapabove5==0
gen married_510=married if gapabove5==1&gapabove10==0
gen married_a10=married if gapabove10==1
sort pupilid
save datasets/homesurvey_rev3, replace

egen Nobs=count(class) if sampleSD==0&class==8
collapse (mean) Nobs age agefather wantedpreg gapabove5 gapabove10 rape cashbefore stillsupporting married_u5 married_510 married_a10 if sampleSD==0&class==8
outsheet using datasets/results/basestatpreg.xls,replace
clear
use /datasets/homesurvey_rev3,clear
egen Nobs=count(class)
collapse (sd) Nobs age agefather wantedpreg gapabove5 gapabove10 rape cashbefore stillsupporting married_u5 married_510 married_a10 if sampleSD==0&class==8
outsheet using datasets/results/basestatpregsd.xls,replace



************************************************** *****************
************************************************** *****************
*********** TABLE 2 ************************************************
************************************************** *****************
************************************************** *****************


* columne 1&2
use /datasets/SDcontrolcohort_rev2, clear
collapse HIVtreat butere bungoma sampleSD clsize girl8perboy8 teacherperpupil fempermaleteacher kcpe2003 repeat8 secschool training athome married fertafter12 Nobs, by(schoolid girl)
bys girl sampleSD : egen Nschools=count(schoolid)
bys girl sampleSD: egen Ntotalobs=sum(Nobs)
collapse (mean) clsize girl8perboy8 teacherperpupil fempermaleteacher kcpe2003 repeat8 secschool training athome married fertafter12 Ntotalobs Nschools HIVtreat, by(girl sampleSD)
outsheet using /datasets/results/baselineSD.xls,replace

use /datasets/SDcontrolcohort_rev2, clear
collapse HIVtreat butere bungoma sampleSD clsize girl8perboy8 teacherperpupil fempermaleteacher kcpe2003 repeat8 secschool training athome married fertafter12 Nobs , by(schoolid girl)
bys girl sampleSD : egen Nschools=count(schoolid)
bys girl sampleSD: egen Ntotalobs=sum(Nobs)
collapse (sd) clsize girl8perboy8 teacherperpupil fempermaleteacher kcpe2003 repeat8 secschool training athome married fertafter12 Ntotalobs Nschools HIVtreat, by(girl sampleSD)
outsheet using /datasets/results/baselineSDSD.xls,replace

*column 3
use /datasets/SDcontrolcohort_rev2, clear
collapse HIVtreat butere bungoma sampleSD clsize girl8perboy8 teacherperpupil fempermaleteacher kcpe2003 repeat8 secschool training athome married fertafter12 Nobs , by(schoolid girl)
xi: reg clsize sampleSD
outreg sampleSD using /datasets/results/SDbasediff.xls, nolabel nocons se ctitle ("clsize") 3aster replace
foreach var of varlist girl8perboy8 teacherperpupil fempermaleteacher kcpe2003 repeat8 secschool training athome married fertafter12 {
reg `var' sampleSD if girl==1
outreg sampleSD using /datasets/results/SDbasediff.xls, nolabel nocons se ctitle ("`var'") 3aster append
}
foreach var of varlist repeat8 secschool training athome married fertafter12 HIVtreat {
reg `var' sampleSD if girl==0
outreg sampleSD using /datasets/results/SDbasediff.xls, nolabel nocons se ctitle ("`var'") 3aster append
}