Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stata batch called from Python: Pipe Stata console output to command line

I have a Python script, which calls several Stata do files:

from subprocess import call
Stata_exec = "D:/Stata 12 MP2/StataMP-64.exe"
dofile = "D:/Test.do" 
call( "\"{0}\" do /e \"{1}\"".format(Stata_exec, dofile), shell=True)

Here is a test do file:

/* Merge some big files */

clear *

// Create dataset A (8000 variables, 300 observations)
set obs 300
gen ID = _n
forval i = 1/8000 {
    gen variableA`i' = runiform()
}
tempfile dataA
save "`dataA'"

// Create dataset B (5000 variables, 300 observations)
clear 
set obs 300
gen ID = _n
forval i = 1/5000 {
    gen variableB`i' = runiform()
}

sort ID

// Attempt merge
merge 1:1 ID using `dataA'
exit, clear

I would like to have the progress of the do file piped to the console in real time, so it will be integrated with the other Python output.

Is this possible?

like image 780
Ben Southgate Avatar asked Nov 19 '25 14:11

Ben Southgate


1 Answers

You can use the log command to echo copy of a Stata session to a file, or maybe the file command to write specific messages (like "Data A Created") to a text file. Python should be able to display such files with subprocess.call(["tail", "-F", logfilename])

like image 91
dimitriy Avatar answered Nov 22 '25 02:11

dimitriy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!