Overview:
Windows: Results, Variables, Review, Variable Viewer, Command line, Viewer, Data Browser, Data Editor
Do files are text files which contain a list of Stata commands. When you run a do file, Stata will initiate each command in order. Using do files is a great way to keep track of your commands and is vital to doing reproducible social science.
do filename.doStata filenames are always given .dta file types. If the filename ends in .dta, Stata should be able to read it. To open a Stata dataset, do one of the following:
use [filename.dta], clear in the command line. Replace [filename.dta] with the filepath of your dataset. You can also download datasets and open them directly in Stata by replacing the filename for the URL of the file
clear option tells Stata to open the newuse http://www.sampledata/filename.dta}gss2012_small.dta. Note where you save the file on your computer, and then open the file by utilizing the use commandsave filename.dta, replacefilename_recoded.dta)Stata has a nontrivial learning curve: you may need to hunt down how to perform certain functions. You should familiarize yourself with Stata’s help options.
help [statacommand]help codebook in the command line and hit entercodebook command: a good way to get a sense of the variable. inspect and describe commands also usefultab1 command is one of the most useful command in Stata and shows basic frequencies of the variable. Use nolabel option to see numeric values and nolabel missing to see numeric values without labels and the number of missing cases (the number of observation on which no data is available for that variable)table is a newly reconfigured command in Stata 17 which allows easy-ish exporting of tables to Word, Excel, Markdown, LaTeX or other document creation programs. We’ll mostly use table for our table creation to automate the process of creating tables for various publications we might want to use
table [varname], statistic(freq) statistic(percent)list command allows you to display your data in the results window.
list [varnames]list is browse. Use the latter to open up the browser window to see your raw data. browse can also be combined with a list of variable names if you want to view just some of your datasort command: allows you to change the order of observation; useful when combining with list (and others) or to inspect dataset in the data editor
sort [varnames]. Can sort by multiple variables if desiredif
tab1 [var1] if [var2] == value.== means equals or “is set to”==: > (greater than), < (less than), != (not equal to)if always goes before , [options] for the commandifif state == "NJ"Write a do-file that opens the dataset gss2012_small.dta, runs the table command with options to include frequencies and percentages on the variable earthsun, sorts the data by age, runs the table command again on just those older than 50, and saves the dataset to your H:/. Be sure to save your do-file so you will have access to it later.