Stata putexcel:将带星号的数字添加到表格中

问题描述 投票:0回答:1

我想根据 Stata 中的数字创建 Excel 表格。单元格可能有数字和星号(表示统计显着性),例如“0.15***”。

Stata 中的最小示例尝试(如果您想使用它,可以从 c: emp 更改路径):

clear all
sysuse auto.dta    
putexcel set "c:\temp\test.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

我几乎通过手动在字符串末尾添加“***”来实现这一目标,只是数字格式不正确,因为小数点前面缺少 0。显示

-.31***

在 Excel 中(单元格 A1)。有没有办法修复这种格式?这将是最简单的解决方案。

然后我尝试了另一种方法(单元格 B1 和 C1),该方法在 Stata 中有效,但创建了一个错误的 Excel 文件,并出现以下错误: enter image description here 说“是”后,星星不会出现在 Excel(单元格 B1)中,但单元格 C1 完全正确,这意味着由于某种原因星星弄乱了创建的 Excel 文件,但该命令适用于其他字符。有什么方法可以解决这个问题,尽管它看起来更像是 Excel 问题而不是 Stata 问题?

最后,我尝试了一些字符串函数魔术(与上面的代码一起使用):

//third attempt using string functions
if substr("`diffrounded'",1,2)=="-." {
    local temp `substr("`diffrounded'",2,.)'
    local diffrounded "-0`temp'***"
}    
putexcel D1 = "`diffrounded'"

Excel 中的单元格显示“-0***”,因此显然原始数字没有放回。

任何对任何尝试的修复都将非常感激!

PS:当然,星星是统计测试的结果。后期在实际应用中;这些将根据统计测试中的 p 值添加到数字中 - 我在这里省略了这一点以使示例更短。

stata
1个回答
0
投票

我想通了。线

putexcel B1 = `diffrounded', nformat("0.00***") //causes excel error

应该是

putexcel B1 = `diffrounded', nformat(0.00"***") //works

第一个版本在Excel中导致错误的原因是,这实际上不是作为字符串放入的,而是作为“数字格式”的数字放入的,使用*是非法的,但使用“*”是可以的。

© www.soinside.com 2019 - 2024. All rights reserved.