I want to create a table for excel, based on numbers from Stata. A cell might have a number and stars (indicating statistical significance), such as “0.15***”.
My attempts in Stata (potentially change path from c:temp if you want to use it):
clear all
sysuse auto.dta
putexcel set "c:temptest.xlsx", sheet(Sheet1) replace //create file
//just create some numbers
quietly sum price if foreign==0
local meandomestic=`r(mean)'/1000
quietly sum price if foreign==1
local meanforeign=`r(mean)'/1000
local diffrounded=round(`meandomestic'-`meanforeign',0.01)
//manually add stars in a string
local output "`diffrounded'***"
putexcel A1 = "`output'" //yields wrong number formatting
//second attempt
putexcel B1 = `diffrounded', nformat("0.00***") //causes excel error
putexcel C1 = `diffrounded', nformat("0.00+++") //doesn't cause excel error
I almost got there by manually putting “***” at the end with a string, except that the number formatting isn’t right, as a 0 in front of the decimal is missing. It displays
-.31***
in excel (cell A1). Is there a way to fix this formatting? That would be the easiest solution.
I then tried another way (cell B1 and C1), which works in Stata but creates a faulty excel file with the following error:
After saying yes, the stars to not appear in excel (cell B1), but cell C1 is exactly right, which means for some reason the stars mess up the created excel file but the command works with other characters. Any way to fix this, though it appears to be more of an excel issue than a Stata issue?
Finally, I tried some string function magic (use along with above code):
//third attempt using string functions
if substr("`diffrounded'",1,2)=="-." {
local temp `substr("`diffrounded'",2,.)'
local diffrounded "-0`temp'***"
}
putexcel D1 = "`diffrounded'"
The cell in excel displays “-0***”, so clearly the original number isn’t put back in.
Any fix to any attempt would be very much appreciated!