Hello All,

I am running a DO file to create a table of summary statistics for a large number of variable and then displaying their t-test result in a table. My objective is to create publication ready tables directly from Stata in word file, and I am using asdoc file to create it.

I need to use a block of code repeatedly for different type of summary stats. Instead of repeating the block of code, can create a program or a macro to "call" that block within a do file whenever needed?

I tried creating a program but it didn't return the output as expected.

Please see the example here:

sysuse auto,clear
asdoc, row(Dependent variable:domestic or foreign, Domestic mean/frequency, Domestic SD, Foreign mean/frequency, Foreign SD, t-test) title(Summary statistics) save(myfile) replace
asdoc, row( Model independent variables, \i, \i, \i, \i, \i) append
foreach var of varlist price mpg rep78 headroom trunk weight length turn{
qui sum `var' if foreign==0
local mf=`r(mean)'/`r(N)'
asdoc, accum(`mf', `r(sd)')
qui sum `var' if foreign==1
local mf=`r(mean)'/`r(N)'
asdoc, accum(`mf', `r(sd)')
ttest `var', by(foreign)
local t=round(abs(`r(t)'),0.001)
local t=substr("`t'",1,5)
if `r(p)'<=0.01 {
local star "***"
}
else if `r(p)'<=0.05{
local star "**"
}
else if `r(p)'<=0.1{
local star "*"
}
else {
local star " "
}
asdoc, accum(`tstar')
asdoc, row(`var', $accum)
}

This creates my required table.

For different summary stats, I need to use the following block of code:
if `r(p)'<=0.01 {
local star "***"
}
else if `r(p)'<=0.05{
local star "**"
}
else if `r(p)'<=0.1{
local star "*"
}
else {
local star " "
}

Is there a way to "call" this block?

I tried creating a program, and then introduce the program in my main do file. However, doing this completely skips the t-star routine and doesn't show any values in the doc file.

cap program drop pstars
program define pstars
if `r(p)'<=0.01 {
local star "***"
}
else if `r(p)'<=0.05{
local star "**"
}
else if `r(p)'<=0.1{
local star "*"
}
else {
local star " "
}
end

sysuse auto,clear
asdoc, row(Dependent variable:domestic or foreign, Domestic mean/frequency, Domestic SD, Foreign mean/frequency, Foreign SD, t-test) title(Summary statistics) save(myfile) replace
asdoc, row( Model independent variables, \i, \i, \i, \i, \i) append
foreach var of varlist price mpg rep78 headroom trunk weight length turn{
qui sum `var' if foreign==0
local mf=`r(mean)'/`r(N)'
asdoc, accum(`mf', `r(sd)')
qui sum `var' if foreign==1
local mf=`r(mean)'/`r(N)'
asdoc, accum(`mf', `r(sd)')
ttest `var', by(foreign)
local t=round(abs(`r(t)'),0.001)
local t=substr("`t'",1,5)
pstars
asdoc, accum(`tstar')
asdoc, row(`var', $accum)
}

Appreciate help.