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
Simple percentiles using wide datasetHello, I'm not sure why I am having such trouble creating a variable corresponding to percentiles. …
Regarding generating year-sum for each variableDear, I have a question. I have a panel data looks like below. Code: country year x y "I…
Getting the repeated-measures bse() error (again)Hello, I'm new to the list so apologies for posting a basic question. Until the latest update of ST…
Matching several observations via new variableHello everyone, Currently I'm working with a dataset that contains several waves with various ID's …
Checking DuplicationsI am trying to use xtset and I know how to remove any duplications. Is there a way to see if the non…
Subscribe to:
Post Comments (Atom)
0 Response to Calling a block of code within a DO file
Post a Comment