A simple example
Inputting data, generating a new variable, listing and saving data, log files.
Start Stata by double-clicking on the desktop icon. Type each of these commands in the Stata Command window, one at a time, pressing Enter after each line:
input a b c 1 5 10 0 8 7 1 4 6 end generate d=c-b list
1. The input command puts data into Stata's memory. What is the result of the input command? In other words, where are those numbers stored? Answer.
2. What does the generate command do? Answer.
3. The list and browse commands list the contents of Stata's memory. Where are the results of all Stata commands displayed? Answer.
4. The save command writes a permanent disk file. Where is the file "myfile" created? Answer.
5. What does the file "myfile" contain? Answer.
6. What are the three most important default actions of a simple Stata program like the one above? Answer.
7. What would you change in the program to make it read one thousand records placed after the input command instead of three? Answer.
generate d=c-b input a b c 1 5 10 0 8 7 1 4 6 end
input a b c 1 5 10 0 8 7 1 4 6 end generate d=C-b
1. The numbers are stored in the desktop PC memory (RAM). There is no file, temporary or permanent, created on disk. The data are stored conceptually as 3 rows (observations) and 3 columns (variables), like an Excel spreadsheet.
2. It creates a 4th column (variable) named "d" with one value for each observation.
3. Stata echos each command that you type and displays the results in the Stata Results window. An exception is browse, which opens a new window. The log command will create a permanent file on disk with the contents of the Stata Results window while you type commands.
input a b c 1 5 10 0 8 7 1 4 6 end generate d=c-b log using mylog.log list log close save myfile
Here the log command creates a file named mylog.log containing the results of the list command but none of the other contents of the Stata Results window.
4. The save and log commands create files in the present working directory (pwd). This directory is displayed in the lower left corner of the Stata window. You can change it with the cd command. You can also specify a full path name in the save and log commands. Stata automatically adds the suffix ".dta" to files created with the save command.
5. The save command creates a Stata-format file. It contains the contents of Stata's memory at the time you type the save command. In this case, the file would have 3 obs and 4 variables.
- all file-related commands (save, log) work in the present working directory
- all variable-related commands (generate) affect every observation in Stata's memory
- commands are executed in order from the top down
infile a b c using "e:/student/jdoe/myproject/data/part1.dat"
Note the quotes around the path and file name. Quotes are only necessary if a directory has blank spaces in it, but it's a good habit to get into. Also, note that the "/" (slashes) can be either forward or backward - Stata doesn't distinguish between them.
9. The generate command results in an error message "c not found", since Stata doesn't yet know about the variable c.
10. The generate command results in an error message "C not found", since Stata distinguishes between upper and lower case.