0

I am new to Python, and am trying to execute a Stata do-file from Python, using MacOS.

Following advice in this question: Run Stata do file from Python

I tried the following code:

dofile = folder_path + "stata_dofile.do"
cmd = ["C:/Program Files (x86)/Stata14/StataSE-64", "do", dofile, variable1, variable2]
subprocess.call(cmd) 

where "dofile" is the Stata do file I want to execute, and "variable1" and "variable2" are some string variables I want to feed to Stata.

This code works fine on Windows. But when I try to adapt it to the Mac as follows:

dofile = folder_path + "stata_dofile.do"
cmd = ["/Applications/Stata/StataMP.app", "do", dofile, variable1, variable2]
subprocess.call(cmd) 

I get an error message "permission denied."

Following some other advice on this forum, I did manage to open the Stata app from Python on my Mac, using the following code:

cmd = ["/usr/bin/open", "/Applications/Stata/StataMP.app"]
subprocess.call(cmd)

But this only opens Stata, and I don't know how to modify the code to make it execute the do-file above.

Basically I have no idea what I'm doing. Would really appreciate any tips!

Nick Cox
  • 30,617
  • 6
  • 27
  • 44

1 Answers1

0

I figure this out!

Suppose we have a test.do file in desktop like this:

sysuse auto, clear

fsum price mpg, s(mean median sd max min) f(%12.3f)

We should not use StataSE.app, instead, we should open its package contents:

import subprocess

cmd = ["/Applications/Stata/StataSE.app/Contents/MacOS/StataSE", "do", "/Users/apple/Desktop/test.do"]

subprocess.call(cmd) 

I hope this would be helpful