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.
Related Posts with Calling a block of code within a DO file
Issues with Lincom commandHello, I posted this question a few days ago, and I am trying to use the lincom command to find the …
Heckman selection model in three stagesDear Statalist, I hope you are well. Please, I would like to ask regarding the Heckman selection mod…
Tokenize and looping through tokensHi Statlist, I have the following problem. I have defined 3 local variables defining the path to so…
Reporting weighted vs. unweighted data in descriptive analysisDear Stata Community: I am using a large nationally representative dataset where data is nested (ch…
Should I rename all the variables with the year immediately following the original variable names when I merge data across years with loopI run the following code: local i = 1998 while `i' < 2007{ local j=`i'+1 **step 10: match by …
Subscribe to:
Post Comments (Atom)
0 Response to Calling a block of code within a DO file
Post a Comment