top of page

Automation in Stata 2022 SDAS Webinar - Do-Files

Updated: Oct 5, 2022

I have included below the do-files for all of the examples that we covered in the Automation webinar. The do-files are all in text file format (.txt) as the .do format is not recognised for upload. You can open them in Stata as-is, or you can change the .txt suffix to .do manually through File Explorer (Windows) or Finder (Mac).

Example 1 - A Simple Do-File:

Download TXT • 46B

Example 2 - Nested Do-Files:

Download TXT • 47B

Download TXT • 9B

Download TXT • 8B

Example 3 - Macros in Stata

This was an interactive example. I have included the commands that were run in this example below with some additional comments:

local item = 1
display `item'
local item = "one"
display `item' //Gives an error, as it is interpreted as a variable name
display "`item'"
global item = 1
display $item //This is a global macro, as different from below
display "`item'" //This is a local macro, as different from above
display "What is your name? " _request(name) // response stored in $name
display "$name"

Play around with macros to get a good feel for how they work.

Example 4 - Positional Arguments Do-File:

Download TXT • 42B

Example 5 - Automated Analysis:

Download TXT • 412B

It is highly recommended that you define all your positional arguments as local macros at the beginning of your do-file, something I neglected to do here. The do-file will run just as well if you don't do this, but it can sometimes make it difficult to determine which positional arguments you have used where.

Example 6 - Automated Reporting:

Download TXT • 9KB

Download XLSX • 6KB

Download XLSX • 6KB

I have included several datasets (as excel files) that this report will run on given the right positional arguments. You will notice for this report that I do define the positional arguments as local macros at the start. Were I to create a report like this in earnest I would likely have separated parts of it out into separate do-files and taken advantage of Stata's ability to nest do-files. In this case as an example it suited me to have it all in one file.

Example 7 - Scheduled Automation

Download TXT • 673B

I have included the batch file (as .txt) that we created for our scheduled job using Task Scheduler in Windows. To use this file on Windows it needs to be re-saved as a batch file (.bat extension). You would also need to make sure the files from Example 6 above had the correct names, were saved as their respective Stata equivalent (.do or .dta), and that the file paths were updated for your computer. You can test a batch file by just double-clicking it. It doesn't need to be scheduled to run, so you are able to check it works before scheduling. Or if you have a report you want to schedule but it only needs to be run sporadically, you can set up a batch file and just double-click to run when you need it to run.

If you have any questions about any of the items here you can email me at

201 views0 comments

Recent Posts

See All
bottom of page