Package: pytask-stata
pytask-stataRun Stata's do-files with pytask.Installationpytask-stata is available onPyPIandAnaconda.org. Install it with$pipinstallpytask-stata#or$condainstall-cconda-forgepytask-stataYou also need to have Stata installed on your system and have the executable on your
system's PATH. If you do not know how to do it,hereis an explanation.UsageSimilarly to normal task functions which execute Python code, you define tasks to
execute scripts written in Stata with Python functions. The difference is that the
function body does not contain any logic, but the decorator tells pytask how to handle
the task.Here is an example where you want to runscript.do.importpytask@pytask.mark.stata(script="script.do")@pytask.mark.produces("auto.dta")deftask_run_do_file():passWhen executing a do-file, the current working directory changes to the directory where
the script is located. This allows you, for example, to reference every data set you
want to read with a relative path from the script.Dependencies and ProductsDependencies and products can be added as with a normal pytask task using the@pytask.mark.depends_onand@pytask.mark.producesdecorators. which is explained in
thistutorial.Accessing dependencies and products in the scriptThe decorator can be used to pass command line arguments to your Stata executable. For
example, pass the path of the product with@pytask.mark.stata(script="script.do",options="auto.dta")@pytask.mark.produces("auto.dta")deftask_run_do_file():passAnd in yourscript.do, you can intercept the value with* Intercept command line argument and save to macro named 'produces'.argsproducessysuseauto,clearsave"`produces'"The relative path inside the do-file works only because the pytask-stata switches the
current working directory to the directory of the do-file before the task is executed.To make the task independent from the current working directory, pass the full path as
an command line argument. Here is an example.# Absolute path to the build directory.fromsrc.configimportBLD@pytask.mark.stata(script="script.do",options=BLD/"auto.dta")@pytask.mark.produces(BLD/"auto.dta")deftask_run_do_file():passRepeating tasks with different scripts or inputsYou can also parametrize the execution of scripts, meaning executing multiple do-files
as well as passing different command line arguments to the same do-file.The following task executes two do-files which produce different outputs.foriinrange(2):@pytask.mark.task@pytask.mark.stata(script=f"script_{i}.do",options=f"{i}.dta")@pytask.mark.produces(f"{i}.dta")deftask_execute_do_file():passConfigurationpytask-stata can be configured with the following options.stata_keep_logUse this option to keep the.logfiles which are produced for every task. This option
is useful to debug Stata tasks. Set the option via the configuration file with[tool.pytask.ini_options]stata_keep_log=trueThe option is also available in the command line interface via the--stata-keep-logflag.stata_check_log_linesUse this option to vary the number of lines in the log file which are checked for error
codes. It also controls the number of lines displayed on errors. Use any integer greater
than zero. Here is the entry in the configuration file[tool.pytask.ini_options]stata_check_log_lines=10and here via the command line interface$pytaskbuild--stata-check-log-lines10ChangesConsult therelease notesto find out about what is new.