Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
DIME Continued Education - November 08, 2018

How to write programs (also called commands or functions) in Stata

This is an introduction to how to write programs in Stata. To try this out yourself, download this Gist by clicking Download Zip. Unpack the .zip file and edit the path global in the runfile.do. Then follow these instructions and run the corresponding section of the run file.

If you have any questions or want us to add another examples on how you can expand myhist.ado as us in the comment section below.

Hello World

It is common in computer science that the first function/command you write is a simple command that just display the message Hello World!. That is what the program in hello_world.ado does.

myhist.ado

Here we are set-by-step writing a command that takes a variable and creates a histogram with that variable. In a real life scenario this command could be expanded so that the command includes a lot of histogram options so that you can easily apply them to many histograms by typing myhist varA, myhist varB, myhist varC etc.

myhist_v1.ado

This is the simplest possible version of this command. The syntax takes exactly one variable (because we use varname instead of varlist) and create a histogram of the distribution in that variable.

myhist_v2.ado

In this version we have added one option called title() that takes a string. We add "DIME -" in front of that string and then use it as the title of the historgram.

myhist_v3.ado

What you call your options does not matter at all to Stata. myhist_v3.ado gives the exact same result as myhist_v2.ado, but the title() option is called xyz(). You should always chose a name that make sense to humans as it will make it easier for humans to read the code - so xyz is a terrible name - but it makes no difference to Stata.

cap program drop hello_world
program define hello_world
display "Hello World!"
end
cap program drop myhist_v1
program define myhist_v1
display "command works!"
*Input
syntax varname
display "syntax works!"
*Process
//Nothing here as this command is themost simple version of histograms
*Output
histogram `varlist'
end
cap program drop myhist_v2
program define myhist_v2
display "command works!"
*Input
syntax varname, [title(string)]
display "syntax works!"
*Process
local title = "DIME - `title'"
*Output
histogram `varlist', title(`title')
end
cap program drop myhist_v3
program define myhist_v3
display "command works!"
*Input
syntax varname, [xyz(string)]
display "syntax works!"
*Process
local xyz = "DIME - `xyz'"
*Output
histogram `varlist', title(`xyz')
end
*Change file path to whereever you save the ado-files
local path "C:\Users/username/Desktop"
do "`path'/hello_world.ado"
do "`path'/myhist_v1.ado"
do "`path'/myhist_v2.ado"
do "`path'/myhist_v3.ado"
*Run the hello world program, this should only print the text "Hello World!"
hello_world
*Load test data (this data is a test data set included in everyone's version of Stata)
sysuse census.dta
*Run version 1
myhist_v1 pop
*Run version 2
myhist_v2 pop, title("TeSt")
*Run version 3
myhist_v3 pop, xyz("TeSt")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment