benjann / estout

Stata module to make regression tables
http://repec.sowi.unibe.ch/stata/estout/index.html
MIT License
70 stars 17 forks source link

esttab csv onecell linebreaks #28

Closed johnhenrypezzuto closed 2 years ago

johnhenrypezzuto commented 3 years ago

I have a simple regression model I want to export to an csv file.

In my table it is crucial the the coefficient and the SE are in the same cell. However, I'd like to also include a linebreak between them. How can I do this while still using the csv format?

sysuse auto, clear
eststo: regress price weight mpg
esttab using example, csv replace ///
onecell ///
incelldelimiter(CHAR(10))
benjann commented 3 years ago

You can type

incelldelimiter(`=char(10)')

to insert a linebreak. The question, however, is whether the program you use to read the resulting CSV will process these linebreaks correctly. I tried with MS Excel, without success. I then looked at how Excel itself stores CSV. It seems that if one uses "Save As..." -> "CVS UTF-8" then MS Excel will preserve the within-cell linebreaks if reopening the file. I tried to mimic this as follows:

sysuse auto, clear
regress price weight mpg
esttab using example.csv, replace ///
    onecell incelldelimiter(`=char(10)') ///
    begin(`"""') delimiter(`"",""') end(`""`=char(13)'"') ///
    prehead(`=uchar(65279)')

This seems to work, sort of. I am using Mac OS; on Windows it may suffice to type

end(`"""')

instead of

end(`""`=char(13)'"')`

I am sorry not to have a better solution.

benjann commented 3 years ago

Add option se.

johnhenrypezzuto commented 3 years ago

Can I follow up with one more question about using stars with negative coefficients? It seems like excel automatically formats the cell when there are negative coefficients but not when there are positive ones.


sysuse auto, clear
eststo clear
eststo: reg price weight
eststo: reg foreign weight
esttab using example, csv replace ///
    onecell incelldelimiter(`=char(10)') ///
    begin(`"""') delimiter(`"",""') end(`""`=char(13)'"') ///
    prehead(`=uchar(65279)') ///
    star(* .01)

image (e.g., the weight coefficient in column 2)

I also did not have any luck trying star(`=char(42)' .01) Is there anything I can add to format the stars to render my results correctly?

benjann commented 2 years ago

Don't really know. The behavior of excel when opening CSV can be hard to control, sometimes even depends on language settings of your computer etc. By default, esttab formats the cells in the CSV as ="..." to signal to excel that it should interpret the contents as text and not change anything. However, this does not seem to work with line breaks within cells. In your case, you format the cells as "...", which works well in most cases (including line breaks), but in some cases excel tries to be smart. I don't know whether there is a way to prevent excel from doing this. In general it is much better to use a tool that directly writes excel format (see help putexcel) instead of using CSV. Unfortunately, estout has no support for putexcel (putexcel did not exist when estout was written).