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
browse
 save myfile

 

Questions:

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.


8. What would you change in the program if your data records were in a file separate from the program? Assume your data file has the following path:

       e:\student\jdoe\myproject\data\part1.dat

Answer.


9. Describe the result if you typed the generate command before the input command:

       generate d=c-b
       input a b c
       1 5 10
       0 8 7
       1 4 6
       end

Answer.


10. Describe the result if you typed the variable name "C" in caps in the generate command.

       input a b c
       1 5 10
       0 8 7
       1 4 6
       end
       generate d=C-b

Answer.

 

 

 


Answers:

 

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.

Back to question. 

 


 

2. It creates a 4th column (variable) named "d" with one value for each observation.

Back to question. 

 


 

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.

Back to question. 

 


 

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.

Back to question. 

 


 

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.

Back to question. 

 


 

6. Default actions:

  • 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

Back to question.

 

 


 

7. Nothing.

Back to question. 

 


 

8. Replace the input command with the infile command:

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.

Back to question. 

 


 

9. The generate command results in an error message "c not found", since Stata doesn't yet know about the variable c.

Back to question. 

 


 

10. The generate command results in an error message "C not found", since Stata distinguishes between upper and lower case.

Back to question. 

 


Review again?

 

Another topic?


Wink Plone Theme by Quintagroup © 2013.

Personal tools
This is themeComment for Wink theme