benjann / estout

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

Windows network paths confuse esttab #58

Closed larsvilhuber closed 1 year ago

larsvilhuber commented 1 year ago

When using esttab with a Windows computer using a network path, it seems to sanitize the path in a way that makes it fail.

Example:

    *Output 
        esttab balance_* using "$tabs\Main_Tables\Tab1_BaselineBalance.csv", ///
        cells(b(star fmt(%9.6f)) se(fmt(%9.6f))) order(_cons treatment) ///
        stardetach stats(Mean Sd N, fmt(%9.3g)) style(tab) varwidth(8) ///
        modelwidth(8) plain replace

where

global root "\\rschfs1x\userrs\xxxxx\Desktop\workspace\yyyyy"
global tabs "$root\SUBMITTED_V3\Output\Tables"

(and SUBMITTED_V3\Output\Tables exists) Typical output is

. which estout
\\rschfs1x\userrs\xxxx\Desktop\workspace\yyyy/ado/plus\e\
> estout.ado
*! version 3.31  26apr2022  Ben Jann

but esttab fails with

file
    \rschfs1x\userrs\F-J\xxxxx\Desktop\workspace\yyyyy\SUBMIT
    > TED_V3\Output\Tables\Main_Tables\Tab1_BaselineBalance.csv not found)
file
    \rschfs1x\userrs\F-J\xxxxx\Desktop\workspace\yyyyy\SUBMIT
    > TED_V3\Output\Tables\Main_Tables\Tab1_BaselineBalance.csv could not be
    opened

(note the \rschfs1x instead of the \\rschfs1x). This only appears to happen with esttab, and can be avoided (on the same computer) by using global root "U:/Desktop\workspace\yyyyy" instead. However, this is not desirable, because the automatic detection of pathnames yields the network path, not the Drive-letter path (to avoid the problem means hard-coding paths all over the place).

My suspicion is that something related to the LaTeX processing of esttab is "sanitizing" the path before attempting to write it out.

Happy to help debug on a Windows computer with such network paths, if not available to you.

larsvilhuber commented 1 year ago

Our guidance to RAs that specifically addresses this is here: https://github.com/labordynamicsinstitute/replicability-training/wiki/Stata-Tips#file-___-could-not-be-opened-error

Note that another workaround is to cd to the relevant directory first (while keeping the network-drive notation), then the command runs fine. Again, this is not a sustainable solution (cd-ing all over the place is not desirable either).

NilsEnevoldsen commented 1 year ago

I wonder if there's another workaround that might work. When Stata encounters a file path with forward slashes, if it is on Windows, it will transparently interpret them as backslashes. This is because a folder like Dropbox/BigProject/main.do on macOS will appear on Windows as Dropbox\BigProject\main.do. In my collaborative projects, I always encourage my team members to use forward slashes everywhere, because Stata will not interpret backslashes as forward slashes on non-Windows operating systems.

However, I have not tried this with double-backslash network paths.

In other words, what happens if you set

global root "//rschfs1x/userrs/xxxxx/Desktop/workspace/yyyyy"
global tabs "$root/SUBMITTED_V3/Output/Tables"

or even the weirder

global root "//rschfs1x\userrs\xxxxx\Desktop\workspace\yyyyy"
global tabs "$root\SUBMITTED_V3\Output\Tables"
larsvilhuber commented 1 year ago

@NilsEnevoldsen All of those work, but that's not the problem.

The first problem is that esttab (and only esttab) removes one of the double slashes, which is inappropriate here. Every other use of the path $root however written works. Which suggests that there is some esttab-specific string processing of the output file path happening.

The second problem is we never actively set the path. Stata does:

global root : pwd

So we only go in and actively hard-code a path when somebody uses esttab. And only then.

larsvilhuber commented 1 year ago

PS: MWE with Linux, Windows with drive letters, and Windows with network drives log files: https://github.com/larsvilhuber/mwe-esttab-issue58

benjann commented 1 year ago

Hi, the issue is that in Stata's macro expansion backslash sometimes has a meaning; an effect of this is that \\ typically expands to \. The double backslash can be preserved by using `macval(lname)' for macro expansion instead of `lname'. In the latest commit I now changed estout and esttab to use macval() with all macros containing file names. Please let me know if Windows network paths work now.

larsvilhuber commented 1 year ago

I ran the MWE with the latest update, and it works! Thanks for solving it (and now I learned yet another thing about Stata programming).

Here's the successful logfile:

https://github.com/larsvilhuber/mwe-esttab-issue58/blob/main/logs/logfile__9_Apr_2023-20_23_39-lv39_RS.log#L85

(you can see the version of estout used in https://github.com/larsvilhuber/mwe-esttab-issue58/blob/main/logs/logfile__9_Apr_2023-20_23_39-lv39_RS.log#L46)

From my point of view, this can be closed.