A Brief Introduction to Stata(1). 1. Getting Started.

30
A Brief Introduction to Stata(1)

Transcript of A Brief Introduction to Stata(1). 1. Getting Started.

Page 1: A Brief Introduction to Stata(1). 1. Getting Started.

A Brief Introduction to

Stata(1)

Page 2: A Brief Introduction to Stata(1). 1. Getting Started.

1. Getting Started1. Getting Started

Page 3: A Brief Introduction to Stata(1). 1. Getting Started.

1.1 Starting STATA 1.2 Running Stata 1.3 Open and Save Datasets in Stata 1.4 Exiting STATA 1.5 Getting help and information 1.6 Notes on STATA Commands 1.7. Entering Data into STATA

Page 4: A Brief Introduction to Stata(1). 1. Getting Started.

1.1 Starting STATA Start a STATA session by performing either of t

he following three procedures: double-click the STATA icon in your desktop. find the STAR button . Use Windows Explorer to navigate across the S

TATA8 folder and find the button of the executable file named wsestata. Double click on this button.

Page 5: A Brief Introduction to Stata(1). 1. Getting Started.

STATA 的用户界面 菜单和工具栏

已执行过的命令窗口

变量窗口

结果输出窗口

命令窗口

Page 6: A Brief Introduction to Stata(1). 1. Getting Started.

STATA windows Stata Command Stata Results Review Variables

Page 7: A Brief Introduction to Stata(1). 1. Getting Started.

1.2 Running Stata A command may be typed in the Stata Comma

nd window and executed by press the Enter key.

Stata running can be interrupted by holding down Ctrl and pressing Pause/Break key.

Page 8: A Brief Introduction to Stata(1). 1. Getting Started.

It is good to open a log_file, press the button or use ‘.log using filename’. When you want to close the file,use ‘.log close’.

Append onto the end of the current log. Type .log using myses, append.

Discard the old log and replace it with the new one. Type .log using myses, replace.

The first choice is probably the best.

Page 9: A Brief Introduction to Stata(1). 1. Getting Started.

You can find out what directory you're in by typing . pwd F:\StataStuff

If you wanted to make a dirctory on the F drive using Stata, type: . cd F:\ F:\

Page 10: A Brief Introduction to Stata(1). 1. Getting Started.

Do-file

Do-file editor Running do-file

Button Do dofile

Page 11: A Brief Introduction to Stata(1). 1. Getting Started.

Data editor Data Browser Data Editor

Rows represent obserbvation.Columns represent variables.

Page 12: A Brief Introduction to Stata(1). 1. Getting Started.

1.3 Open and Save Datasets in Stata Stata has its own data format with default ext

ension .dta .

Page 13: A Brief Introduction to Stata(1). 1. Getting Started.

Open a data file If the file is in the current directory, .Use filename If the data are not stored in the current directo

ry, .use c:\user\data Or .use c:\user\data,clear In the first form, use filename says to load the

data, but Stata will refuse if (1) there is something in memory and (2) that something has not been saved on disk.

Page 14: A Brief Introduction to Stata(1). 1. Getting Started.

If the memory allocated to STATA (which is 1,000K or 1M by default) is too little for the data file to be opened we will see an error message like the following: . use hh no room to add more observations r(901);

The following commands allocate 30M to STATA and then tries again to open the file: . set memory 30m . use hh

If you continue to get an error message, you can try 40m or 50m.

Page 15: A Brief Introduction to Stata(1). 1. Getting Started.

Another important thing to remember is that the memory allocation command (set memory 30m for example) works only if no data set is open. Otherwise you will get following error message: .use hh .set memory 30m no; data in memory would be lost r(4);

You can clear the memory using one of the two commands: clear or drop _all.

Page 16: A Brief Introduction to Stata(1). 1. Getting Started.

Save a data file To save, type:

. save 1998Data . save 1998Data, replace . Save,replace

Without the replace option, the file must not already exist. If it does, Stata will refuse to overwrite it.

With the replace option, the file may exist or not. If the file does exist, it will be overwritten.

Page 17: A Brief Introduction to Stata(1). 1. Getting Started.

.cd c:\user\data .use bank .save bank

Page 18: A Brief Introduction to Stata(1). 1. Getting Started.

1.4 Exiting STATA Stata may be exited in three ways:

Click into the Close button Select the File menu from the menu bar a

nd select Exit Type. Exit or . exit, clear in the Stata comm

and windows and press return.

Page 19: A Brief Introduction to Stata(1). 1. Getting Started.

if you have an unsaved data set open, STATA will issue the following error message: . exit no; data in memory would be lost r(4);

To remedy this problem you can save the data file and then issue the exit command. If you really want to exit STATA without saving the data file:

. exit, clear

Page 20: A Brief Introduction to Stata(1). 1. Getting Started.

1.5 Getting help and information Stata's help system consists of two commands:

1) .help 2) .search

You use help when you know the name of the Stata command on which you want information. This command only works if you type the full command name or keyword unabbreviated.

Page 21: A Brief Introduction to Stata(1). 1. Getting Started.

If you can not recall the full command name or keyword, or you are not sure about which command you want you can use the command lookup or search followed by the command name or keyword. . search mem

The STATA website (http: //www.stata. com) has excellent help facilities, for example, Online Tutorial, Frequently Asked Questions (FAQ), etc.

Page 22: A Brief Introduction to Stata(1). 1. Getting Started.

1.6 Notes on STATA Commands STATA commands are typed in lower case. All names, including commands or variable

names, can be abbreviated as long as there is no ambiguity.

e.g. .describe .des

Page 23: A Brief Introduction to Stata(1). 1. Getting Started.

To display the previous command in Stata Command window, you can press Page-Up key. You can keep doing that until the first command of the session appears. Similarly, the Page-Down key displays the command that follows the currently displayed command in the Stata Command window.

Page 24: A Brief Introduction to Stata(1). 1. Getting Started.

1.7. Entering Data into STATA the simplest way is to enter data directly into

STATA’s spreadsheet-like Data Editor window. The nineth button . edit

Note that data from spreadsheets can also be copied and pasted into the Editor.

Page 25: A Brief Introduction to Stata(1). 1. Getting Started.

you can entering data variable-by-variable with the TAB key; or observation-by-observation with the ENTER key.

a missing value is coded as a period “.” numerical values should not include

commas; otherwise STATA considered these values as string.

Page 26: A Brief Introduction to Stata(1). 1. Getting Started.

string variable values can nearly be almost any combination of letters, numbers, and symbols up to 80 characters in length for Intercooled/ Small and up to 244 characters in length for STATA/SE. http://www.ats.ucla.edu/stat/stata/how_to_get_st

ata.htm variable names can be changed by double clic

king anywhere in the variable‘s column resulting in a dialogue box. It is suggested that variable names should have 8 characters.

Page 27: A Brief Introduction to Stata(1). 1. Getting Started.

Reading different types of datafile

Using Stata transfer Other ways read data into STATA from ASCII files and wor

ksheets, We can use the infile command . cd c:\intropov\data . infile domain hcn age using try.txt

Page 28: A Brief Introduction to Stata(1). 1. Getting Started.

use the infix command . clear . infix domain 1 hcn 2 age 3-4 using try2.txt

Excel worksheets such as the .xls file can also be read into STATA. However, we have to first read the file with Excel, and convert the file into a comma-delimited file, i.e., click on File, Save AS, and choose as file type CSV. Then, enter

. insheet using filename.csv, clear

Page 29: A Brief Introduction to Stata(1). 1. Getting Started.

Review Ctrl + Pause/Break .log using myses, append . pwd . cd F:\ use c:\user\data,clear set memory 30m . save 1998Data, replace . Save,replace

Page 30: A Brief Introduction to Stata(1). 1. Getting Started.

. exit .help .search .describe .des .edit .infile .infix .insheet