1
votes

I want to write a TeX file in Stata which contains a p-value rounded to the nearest .001. Since sutex, outtab, etc doesn't have built-in capabilities for post-estimation results, I've tried a bunch of things like

file write myfile %4z "`r(p)'"

or

gen temp = round( `r(p)',.001)
format temp %4.3f
sum temp, f
file write myfile " & `r(mean)'" 

Any ideas?

1

1 Answers

3
votes

Here is an example which writes the p-value from a t-test into a text file. The p-value is rounded to the third decimal.

sysuse auto, clear
ttest price, by(fore)
file open myfile using "foo.txt", write replace
file write myfile "price & " %5.3f (r(p)) " \\"
file close myfile 
type "foo.txt"

I have specified a 5.3f format. The first number means that the output will have a width of 5. This width includes the decimal point. The second number means that there will be three digits following the decimal point. Have a look at help format if you want to know more about this notation and about other options you have.

The parentheses surrounding r(p) mean that this expression will be avluated and the result will be written into the file. This is explained in help file.