291
STATA STATA : : The The White White tutorial tutorial

STATA White tutorial

  • Upload
    others

  • View
    6

  • Download
    0

Embed Size (px)

Citation preview

Page 1: STATA White tutorial

STATASTATA:: The The WhiteWhite tutorialtutorial

Page 2: STATA White tutorial

This tutorial presentation is prepared by

Mohammad Ehsanul [email protected]

STATASTATA:: The The WhiteWhite tutorialtutorial

Page 3: STATA White tutorial

This tutorial presentation is prepared by

Mohammad Ehsanul [email protected]

STATASTATA:: The The WhiteWhite tutorialtutorial

Page 4: STATA White tutorial

ContentsContents• Introduction to Stata• Stata Forms• About This Document• Starting / Launching Stata • Stata Windows• To Exit STATA• STATA: Help, Search, Tutorials, Manuals, Website• Data Entry• Knowing about the Data set • Variables• Time Variables• STATA system diagnosis

1. Introduction

2. Stata Help3. Stata & Data

4. Stata Variable

5. Stata System

Page 5: STATA White tutorial

• Introduction to Stata• Stata Forms• About This Document• Starting / Launching Stata • Stata Windows• To Exit STATA

1. IntroductionIntroduction

Page 6: STATA White tutorial

Introduction to Stata

Stata is a powerful general purpose • statistical, • data management, and • graphical package

that is used by researchers across a wide array of disciplines

Page 7: STATA White tutorial

Introduction to Stata

Stata is a powerful general purpose • statistical, • data management, and • graphical package

that is used by researchers across a wide array of disciplines, maintained by Stata Corporation.

Page 8: STATA White tutorial

Introduction to Stata

Stata provides the user a wide range of statistical functions, from simple descriptive tasks to the most sophisticated modeling.

Page 9: STATA White tutorial

Introduction to Stata

Stata provides the user a wide range of statistical functions, from simple descriptive tasks to the most sophisticated modeling.

Page 10: STATA White tutorial

Stata Forms

– Intercooled Stata– Small Stata– Stata/SE (Special Edition)

Page 11: STATA White tutorial

Stata Forms

– Intercooled Stata– Small Stata– Stata/SE (Special Edition)

In this documentation, we will describe Intercooled Stata 7 for windows.

Page 12: STATA White tutorial

Stata Forms

– Intercooled Stata– Small Stata– Stata/SE (Special Edition)

In this documentation, we will describe Intercooled Stata 7 for windows. However, the other versions share similar patterns, and commands.

Page 13: STATA White tutorial

About This Document

• In this tutorial we will find key elements of Stata, descriptions and illustrations for some of the important Stata commands as well as a very brief explanation for their purpose.

Page 14: STATA White tutorial

About This Document

• In this tutorial we will find key elements of Stata, descriptions and illustrations for some of the important Stata commands as well as a very brief explanation for their purpose. The basic purpose of this tutorial is to familiarize people of statistics with basic operations of Stata system and learn how to use the basic commands.

Page 15: STATA White tutorial

About This Document

• Most of this tutorial is documented on the purpose of reminding me what I learned; not to make an encyclopedia on Stata.

• The materials of this document is learnt from Stata built-in help files and various internet based free tutorials (official, mirror and/or unofficial materials). It is strongly recommended to go for those original documentations (if possible see official siteresources for latest updates).

Page 16: STATA White tutorial

Starting / Launching Stata

• Under Windows (Windows 95, NT, XP) we start by clicking on the Stata icon on our desktop or via the menu system that is opened if we click the Start bottonon the left-under corner of our screen.

Page 17: STATA White tutorial

Starting / Launching Stata

• Under Windows (Windows 95, NT, XP) we start by clicking on the Stata icon on our desktop or via the menu system that is opened if we click the Start bottonon the left-under corner of our screen.

Page 18: STATA White tutorial

Starting / Launching Stata

• Under Windows (Windows 95, NT, XP) we start by clicking on the Stata icon on our desktop or via the menu system that is opened if we click the Start bottonon the left-under corner of our screen. Note that Unix, Macintosh platform is also available for Stata.

Page 19: STATA White tutorial

Stata Windows

• Stata contains 4 basic windows:

Page 20: STATA White tutorial

Stata Windows

• Stata contains 4 basic windows:1. Stata Command2. Stata Results3. Review4. Variables

Page 21: STATA White tutorial

Stata Windows

• Stata contains 4 basic windows:1. Stata Command2. Stata Results3. Review4. Variables

• Each of these windows can be moved and resized!

Page 22: STATA White tutorial

Stata Windows

Page 23: STATA White tutorial

Stata Windows

Stata Command window

Page 24: STATA White tutorial

Stata Windows

The Stata Command window, located at the right-bottom side of the screen, is where we have to enter and edit, via the keyboard,

Stata’s commands (there is a flashing prompt). Once the appropriate command is typed we have to press the return key

to execute it.

Stata Command window

Page 25: STATA White tutorial

Stata Windows

Page 26: STATA White tutorial

Stata Windows

Stata Results

Page 27: STATA White tutorial

Stata Windows

Stata Results

The Stata Results window, located at the right side of the screen, displays the results of our commands. In

case that there is any error in the command inserted, a message -in red text- will appear.

Page 28: STATA White tutorial

Stata Windows

Page 29: STATA White tutorial

Stata Windows

Review window

Page 30: STATA White tutorial

Stata Windows

Review window

The Review window, located at the upper left side of the screen, lists all the executed commands of the session (both right and false). By clicking on a listed command, this is appeared at the Command Window and we can

either execute it again or modify it.

Page 31: STATA White tutorial

Stata Windows

Page 32: STATA White tutorial

Stata Windows

Variables window

Page 33: STATA White tutorial

Stata Windows

Variables window

The Variables window, located at the left-bottom side of the screen, displays all the variables that the currently used dataset

contains.

Page 34: STATA White tutorial

Stata WindowsSummary

Page 35: STATA White tutorial

Stata Windows

Stata Command window

Summary

Page 36: STATA White tutorial

Stata Windows

Stata Command window

Stata Results

Summary

Page 37: STATA White tutorial

Stata Windows

Stata Command window

Stata Results

Review window

Summary

Page 38: STATA White tutorial

Stata Windows

Stata Command window

Stata ResultsVariables window

Review window

Summary

Page 39: STATA White tutorial

To Exit STATA

To exit Stata, type: . exit at the Stata Command window.

If the dataset has not been saved since changes have been made, Stata will not allow you to exit the program with the simple exit command.

At this point, you may save the dataset and then type: . exit

If you do not want to save the changes to your dataset, type: . exit, clear

and Stata will allow you to exit without saving the changes in the dataset.

Page 40: STATA White tutorial

• STATA: Manuals• STATA: Help • STATA: Search • STATA: Tutorials • STATA: Website

2. Stata HelpStata Help

Page 41: STATA White tutorial

STATA: Help, Search, Tutorials, Manuals, Website

1. STATA MANUALSBase documentation set for STATA consists of the

following manuals:• Stata getting started• Stata User’s guide (More general Desctiption) -

Shortcut: [U]• Stata Base reference manual (4 volume) - Shortcut:

[R]• Stata graphics reference manual - Shortcut: [G]• Stata programming reference manual (Specialized

reference manual)• Stata Cross-sectional time-series reference manual

(Specialized reference manual)

Page 42: STATA White tutorial

STATA: Help, Search, Tutorials, Manuals, Website

2. STATA HELP

Stata has extensive help once we type in the the Stata Command window:

Page 43: STATA White tutorial

STATA: Help, Search, Tutorials, Manuals, Website

. help

Page 44: STATA White tutorial

STATA: Help, Search, Tutorials, Manuals, Website

. help

gives an overview of help in the Stata Results

Type in the Stata Command window

Page 45: STATA White tutorial

STATA: Help, Search, Tutorials, Manuals, Website

. help

Page 46: STATA White tutorial

STATA: Help, Search, Tutorials, Manuals, Website

. help contents

Page 47: STATA White tutorial

STATA: Help, Search, Tutorials, Manuals, Website

. help contents

gives many pages of commands in the Stata Results

Type in the Stata Command window

Page 48: STATA White tutorial

STATA: Help, Search, Tutorials, Manuals, Website

. help contents

Page 49: STATA White tutorial

STATA: Help, Search, Tutorials, Manuals, Website

. help regress

Page 50: STATA White tutorial

STATA: Help, Search, Tutorials, Manuals, Website

. help regress

gives help on the stata command regress for linear regression in the Stata Results

Type in the Stata Command window

Page 51: STATA White tutorial

STATA: Help, Search, Tutorials, Manuals, Website

. help regress

Page 52: STATA White tutorial

STATA: Help, Search, Tutorials, Manuals, Website

3. STATA SEARCH

This can give more results than Stata help

Page 53: STATA White tutorial

STATA: Help, Search, Tutorials, Manuals, Website

. search statistics

Page 54: STATA White tutorial

STATA: Help, Search, Tutorials, Manuals, Website

. search statistics

gives summary of commands for statistical analysis in the Stata Results

Type in the Stata Command window

Page 55: STATA White tutorial

STATA: Help, Search, Tutorials, Manuals, Website

. search statistics

Page 56: STATA White tutorial

STATA: Help, Search, Tutorials, Manuals, Website

. search simulation

Page 57: STATA White tutorial

STATA: Help, Search, Tutorials, Manuals, Website

. search simulation

gives information on simulation in the Stata Results

Type in the Stata Command window

Page 58: STATA White tutorial

STATA: Help, Search, Tutorials, Manuals, Website

. search simulation

Page 59: STATA White tutorial

STATA: Help, Search, Tutorials, Manuals, Website

Also, we can do this as follows

Page 60: STATA White tutorial

STATA: Help, Search, Tutorials, Manuals, Website

Also, we can do this as follows

Page 61: STATA White tutorial

STATA: Help, Search, Tutorials, Manuals, Website

Also, we can do this as follows, for example, for help in Graph

This helps getting the detailed help of that particular command.

Page 62: STATA White tutorial

STATA: Help, Search, Tutorials, Manuals, Website

4. STATA TUTORIALS

Stata gives several tutorials that demonstrate various modules.

Page 63: STATA White tutorial

STATA: Help, Search, Tutorials, Manuals, Website

. tutorial contents

Page 64: STATA White tutorial

STATA: Help, Search, Tutorials, Manuals, Website

. tutorial contents

lists available of turorials in the Stata Results

Type in the Stata Command window

Page 65: STATA White tutorial

STATA: Help, Search, Tutorials, Manuals, Website

. tutorial contents

Page 66: STATA White tutorial

STATA: Help, Search, Tutorials, Manuals, Website

. tutorial survival

Page 67: STATA White tutorial

STATA: Help, Search, Tutorials, Manuals, Website

. tutorial survival

demonstrates survival commands in the Stata Results

Type in the Stata Command window

Page 68: STATA White tutorial

STATA: Help, Search, Tutorials, Manuals, Website

. tutorial survival

Page 69: STATA White tutorial

STATA: Help, Search, Tutorials, Manuals, Website

5. STATA WEB-SITE

The website adderss is

http://www.stata.com/

Page 70: STATA White tutorial

STATA: Help, Search, Tutorials, Manuals, Website

5. STATA WEB-SITE

The website has a lot of information. This includes summary of what Stata does.

Page 71: STATA White tutorial

STATA: Help, Search, Tutorials, Manuals, Website

5. STATA WEB-SITE

The website has a lot of information. This includes summary of what Stata does. If we are connected to the internet, we can also search through internet using stata (if the proxy port is correctly setup and the proxy supports such action).

Page 72: STATA White tutorial

STATA: Help, Search, Tutorials, Manuals, Website

searching through internet using stata

Page 73: STATA White tutorial

STATA: Help, Search, Tutorials, Manuals, Website

searching through internet using stata

Page 74: STATA White tutorial

STATA: Help, Search, Tutorials, Manuals, Website

searching through internet using stata

Page 75: STATA White tutorial

STATA: Help, Search, Tutorials, Manuals, Website

The findit command can be used to searchthe Stata site and other sites for Stata related information, including

• additional programs (ado's), • Stata FAQs, and • other helpful web pages.

Page 76: STATA White tutorial

STATA: Help, Search, Tutorials, Manuals, Website

Say that we read a web page or someone told you about a great program called nmissing that is not built into Stata, but can be downloaded over the internet. we can search for this program from within Stata by typing

Page 77: STATA White tutorial

STATA: Help, Search, Tutorials, Manuals, Website

Say that we read a web page or someone told you about a great program called nmissing that is not built into Stata, but can be downloaded over the internet. we can search for this program from within Stata by typing

. findit nmissing

Page 78: STATA White tutorial

STATA: Help, Search, Tutorials, Manuals, Website

Then Stata Viewer window appears with a number of resources related to nmissing

Page 79: STATA White tutorial

STATA: Help, Search, Tutorials, Manuals, Website

Then Stata Viewer window appears with a number of resources related to nmissing. If we click on the appropriate link, we get a window that shows us information about the program and a link that allows us to download the program.

Page 80: STATA White tutorial

STATA: Help, Search, Tutorials, Manuals, Website

Once we have downloaded the program, it is part of Stata and we can use it like any other command. In this case, we could then type help nmissing to learn more about how to use it.

Page 81: STATA White tutorial

STATA: Help, Search, Tutorials, Manuals, Website

For frequently asked questions see

http://www.stata.com/support/

Page 82: STATA White tutorial

STATA: Help, Search, Tutorials, Manuals, Website

Or we can surf directly from stata

Page 83: STATA White tutorial

STATA: Help, Search, Tutorials, Manuals, Website

Summary of STATA: Help, Search, Tutorials, Manuals, Website:Key Stata Commands we have seen

Page 84: STATA White tutorial

STATA: Help, Search, Tutorials, Manuals, Website

Summary of STATA: Help, Search, Tutorials, Manuals, Website:Key Stata Commands:

. help

. search

. tutorial

. findit

Page 85: STATA White tutorial

STATA: Help, Search, Tutorials, Manuals, Website

Summary of STATA: Help, Search, Tutorials, Manuals, Website:Key Stata Commands:

. help [How to get help from Stata]

. search [How to search stata documants]

. tutorial [How to get tutorials]

. findit [How to search internet for ado]

Page 86: STATA White tutorial

STATA: Help, Search, Tutorials, Manuals, Website

Summary of STATA: Help, Search, Tutorials, Manuals, Website:Key Stata Commands: Examples

. help . help regress

. search . search simulation

. tutorial . tutorial survival

. findit . findit nmissing

Page 87: STATA White tutorial

• Data Entry• Knowing about the Data set

3. Stata & DataStata & Data

Page 88: STATA White tutorial

Data Entry

Stata Data Sets

Stata data sets have the extension .dta

Reading and saving data files of this extension is simple.

Page 89: STATA White tutorial

Data Entry

Opening a Stata Data File

Stata offers windows facilities to load a Stata format file. Click on the open icon at the toolbar (or equivalently scroll down File and select Open)

Page 90: STATA White tutorial

Data Entry

Opening a Stata Data File

Page 91: STATA White tutorial

Data Entry

Opening a Stata Data File

and a Use New Data dialogue box will appear. Then go to the location that contains the Stata format file that we wish to load, select the file

Page 92: STATA White tutorial

Data Entry

Opening a Stata Data File

Page 93: STATA White tutorial

Data Entry

Opening a Stata Data File

and click on the Open button at the bottom right of the dialogue box. This will open the selected stata data file.

Page 94: STATA White tutorial

Data Entry

Opening a Stata Data File

Page 95: STATA White tutorial

Data Entry

Opening a Stata Data File

Or equivalently, to open the data using command, we write “use datafile” in statacommand. For example, suppose that we have a data file world.dta in the default data directory. To open it we writeuse world in stata command.

Page 96: STATA White tutorial

Data Entry

Opening a Stata Data File

Page 97: STATA White tutorial

Data Entry

Opening a Stata Data File

Page 98: STATA White tutorial

Data Entry

Opening a Stata Data File

The variables of this data file appears in the Variables window

Page 99: STATA White tutorial

Data Entry

Opening a Stata Data File

Similarly the Review windowkeeps record of the previous commands

Page 100: STATA White tutorial

Data Entry

Opening a Stata Data File

Page 101: STATA White tutorial

Data Entry

• When the output is longer than the stataresults window, --more-- then comes in the stata result window. Pressing any key (such as space bar) will forward to next screen.

Opening a Stata Data File

Page 102: STATA White tutorial

Data Entry

Opening a Stata Data File

Page 103: STATA White tutorial

Data Entry

Opening a Stata Data File

Page 104: STATA White tutorial

Data Entry

Once data is read in to Stata we should save it in Stata format using the savecommand. If we want to save it in another name, say world2, we just type save world2

in stata command to save it in the default data directory.

Saving Stata Data Set

Page 105: STATA White tutorial

Data Entry

But if we wish to replace the existing data file, we just type save world, replace

in stata command to save it in the default data directory.

Saving Stata Data Set

Page 106: STATA White tutorial

Data Entry

Suppose that we have the following data that we want to type in our computer

147 1 1 3 pub 1 47 62 53 53 61108 0 1 2 pub 2 34 33 41 36 3618 0 3 2 pub 3 50 33 49 44 36

153 0 1 2 pub 3 39 31 40 39 5150 0 2 2 pub 2 50 59 42 53 6151 1 2 1 pub 2 42 36 42 31 39

102 0 1 1 pub 1 52 41 51 53 5657 1 1 2 pub 1 71 65 72 66 56

160 1 1 2 pub 1 55 65 55 50 61136 0 1 2 pub 1 65 59 70 63 51

Inputting data in Stata by a type-writer

Page 107: STATA White tutorial

Data Entry

We type in the Stata Commands as follows. clear

Inputting data in Stata by a type-writer

Page 108: STATA White tutorial

Data Entry

We type in the Stata Commands as follows. clear

To clear the memory to prepare stata for new data set

Inputting data in Stata by a type-writer

Page 109: STATA White tutorial

Data Entry

We type in the Stata Commands as follows. clear. input id female race ses str3 schtype prog read write math

science socst

Inputting data in Stata by a type-writer

Page 110: STATA White tutorial

Data Entry

We type in the Stata Commands as follows. clear. input id female race ses str3 schtype prog read write math

science socst

Providing stata variable names after input command.Here schtype is a non-numerical value. Therefore we used the str3 option.

Inputting data in Stata by a type-writer

Page 111: STATA White tutorial

Data Entry

We type in the Stata Commands as follows. clear. input id female race ses str3 schtype prog read write math

science socst. 147 1 1 3 pub 1 47 62 53 53 61

Inputting data in Stata by a type-writer

Page 112: STATA White tutorial

Data Entry

We type in the Stata Commands as follows. clear. input id female race ses str3 schtype prog read write math

science socst. 147 1 1 3 pub 1 47 62 53 53 61

Then we input data sequentially as:

Inputting data in Stata by a type-writer

Page 113: STATA White tutorial

Data Entry

We type in the Stata Commands as follows. clear. input id female race ses str3 schtype prog read write math

science socst. 147 1 1 3 pub 1 47 62 53 53 61. 108 0 1 2 pub 2 34 33 41 36 36. 18 0 3 2 pub 3 50 33 49 44 36. 153 0 1 2 pub 3 39 31 40 39 51. 50 0 2 2 pub 2 50 59 42 53 61. 51 1 2 1 pub 2 42 36 42 31 39. 102 0 1 1 pub 1 52 41 51 53 56. 57 1 1 2 pub 1 71 65 72 66 56. 160 1 1 2 pub 1 55 65 55 50 61. 136 0 1 2 pub 1 65 59 70 63 51

Inputting data in Stata by a type-writer

Page 114: STATA White tutorial

Data Entry

We type in the Stata Commands as follows. clear. input id female race ses str3 schtype prog read write math

science socst. 147 1 1 3 pub 1 47 62 53 53 61. 108 0 1 2 pub 2 34 33 41 36 36. 18 0 3 2 pub 3 50 33 49 44 36. 153 0 1 2 pub 3 39 31 40 39 51. 50 0 2 2 pub 2 50 59 42 53 61. 51 1 2 1 pub 2 42 36 42 31 39. 102 0 1 1 pub 1 52 41 51 53 56. 57 1 1 2 pub 1 71 65 72 66 56. 160 1 1 2 pub 1 55 65 55 50 61. 136 0 1 2 pub 1 65 59 70 63 51. end

Inputting data in Stata by a type-writer

Page 115: STATA White tutorial

Data Entry

We type in the Stata Commands as follows. clear. input id female race ses str3 schtype prog read write math

science socst. 147 1 1 3 pub 1 47 62 53 53 61. 108 0 1 2 pub 2 34 33 41 36 36. 18 0 3 2 pub 3 50 33 49 44 36. 153 0 1 2 pub 3 39 31 40 39 51. 50 0 2 2 pub 2 50 59 42 53 61. 51 1 2 1 pub 2 42 36 42 31 39. 102 0 1 1 pub 1 52 41 51 53 56. 57 1 1 2 pub 1 71 65 72 66 56. 160 1 1 2 pub 1 55 65 55 50 61. 136 0 1 2 pub 1 65 59 70 63 51. end

Inputting data in Stata by a type-writer

We used the endcommand to let stataknow that the data inputprocess is being terminated

Page 116: STATA White tutorial

Data Entry

Then check its status as:

Inputting data in Stata by a type-writer

Page 117: STATA White tutorial

Data Entry

Then check its status as:. describe

Inputting data in Stata by a type-writer

Page 118: STATA White tutorial

Data Entry

Then check its status as:. describe

Contains data from hsb1.dtaobs: 200 highschool and beyond (200 cases)

vars: 11 12 Apr 2006 22:43size: 9,600 (98.9% of memory free)

---------------------------------------------------------------------

Inputting data in Stata by a type-writer

storage display value

variable name type format label variable label---------------------------------------------------------------------id float %9.0g female float %9.0g fl race float %12.0g rlses float %9.0g slschtyp float %9.0g scl type of schoolprog float %9.0g sel type of programread float %9.0g reading scorewrite float %9.0g writing scoremath float %9.0g math scorescience float %9.0g science scoresocst float %9.0g social studies score---------------------------------------------------------------------Sorted by:

Page 119: STATA White tutorial

Data Entry

Then check its status as:. describe

Contains data from hsb1.dtaobs: 200 highschool and beyond (200 cases)

vars: 11 12 Apr 2006 22:43size: 9,600 (98.9% of memory free)

---------------------------------------------------------------------

Inputting data in Stata by a type-writer

storage display value

variable name type format label variable label---------------------------------------------------------------------id float %9.0g female float %9.0g fl race float %12.0g rlses float %9.0g slschtyp float %9.0g scl type of schoolprog float %9.0g sel type of programread float %9.0g reading scorewrite float %9.0g writing scoremath float %9.0g math scorescience float %9.0g science scoresocst float %9.0g social studies score---------------------------------------------------------------------Sorted by:

Page 120: STATA White tutorial

Data Entry

Then check its status as:. describe. compress

Inputting data in Stata by a type-writer

Page 121: STATA White tutorial

Data Entry

Then check its status as:. describe. compress

id was float now intfemale was float now byterace was float now byteses was float now byteschtyp was float now byteprog was float now byteread was float now bytewrite was float now bytemath was float now bytescience was float now bytesocst was float now byte

Inputting data in Stata by a type-writer

Page 122: STATA White tutorial

Data Entry

Then check its status as:. describe. compress. save hsb1

Inputting data in Stata by a type-writer

Page 123: STATA White tutorial

Data Entry

Then check its status as:. describe. compress. save hsb1

Inputting data in Stata by a type-writer

We used the save command to save the stata data by the name hsb1.dta

Page 124: STATA White tutorial

Data Entry

Here is an example of non-Stata non-delimited Data Set, saved in data directory as hsb0.raw:

Stata Command Infix for Reading Non-Stata non-delimited Data Sets

Page 125: STATA White tutorial

Data Entry

Here is an example of non-Stata non-delimited Data Set, saved in data directory as hsb0.raw:

195 0949512638616194138780081841479700 8705687816369066487182960786 069 08819419392198979090781

107868180801

Stata Command Infix for Reading Non-Stata non-delimited Data Sets

Page 126: STATA White tutorial

Data Entry

To read in the STATA the hsb0.raw file, we separate and count the columns for various variables and type in as follows:

Stata Command Infix for Reading Non-Stata non-delimited Data Sets

Page 127: STATA White tutorial

Data Entry

To read in the STATA the hsb0.raw file, we separate and count the columns for various variables and type in as follows:

. infix id 1-2 a1 3-4 t1 5-6 gender 7 a2 8-9 t2 10-11 tgender 12 using hsb0.raw

Stata Command Infix for Reading Non-Stata non-delimited Data Sets

Page 128: STATA White tutorial

Data Entry

To read in the STATA the hsb0.raw file, we separate and count the columns for various variables and type in as follows:

. infix id 1-2 a1 3-4 t1 5-6 gender 7 a2 8-9 t2 10-11 tgender 12 using hsb0.raw

Stata Command Infix for Reading Non-Stata non-delimited Data Sets

Page 129: STATA White tutorial

Data Entry

To Suppose that we have the following data

.038 4.5

.053 14.5

.077 24.5

.100 34.5

.167 44.5

.250 54.5

.500 105.0

saved by the name nl_test.raw in the default data directory (note that this is a non *.dta extensional file).

Stata Command Infile for Reading Non-Stata delimited Data Sets

Page 130: STATA White tutorial

Data Entry

For free format or unformatted data the basic command is:

Stata Command Infile for Reading Non-Stata delimited Data Sets

Page 131: STATA White tutorial

Data Entry

For free format or unformatted data the basic command is:

. infile v1 v2 using nl_test.raw

Stata Command Infile for Reading Non-Stata delimited Data Sets

Page 132: STATA White tutorial

Data Entry

For free format or unformatted data the basic command is:

. infile v1 v2 using nl_test.raw

. list

Stata Command Infile for Reading Non-Stata delimited Data Sets

Page 133: STATA White tutorial

Data Entry

For free format or unformatted data the basic command is:

. infile v1 v2 using nl_test.raw

. listv1 v2

1. .038 4.5 2. .053 14.5 3. .077 24.5 4. .1 34.5 5. .167 44.5 6. .25 54.5 7. .5 105

Stata Command Infile for Reading Non-Stata delimited Data Sets

Page 134: STATA White tutorial

Data Entry

For free format or unformatted data the basic command is:

. infile v1 v2 using nl_test.raw

. listv1 v2

1. .038 4.5 2. .053 14.5 3. .077 24.5 4. .1 34.5 5. .167 44.5 6. .25 54.5 7. .5 105

Stata Command Infile for Reading Non-Stata delimited Data Sets

For the data.038 4.5.053 14.5.077 24.5.100 34.5.167 44.5.250 54.5.500 105.0

Page 135: STATA White tutorial

Data Entry

For free format or unformatted data the basic command is:

. infile v1 v2 using nl_test.raw

. listv1 v2

1. .038 4.5 2. .053 14.5 3. .077 24.5 4. .1 34.5 5. .167 44.5 6. .25 54.5 7. .5 105

. save nl_test

Stata Command Infile for Reading Non-Stata delimited Data Sets

For the data.038 4.5.053 14.5.077 24.5.100 34.5.167 44.5.250 54.5.500 105.0

Page 136: STATA White tutorial

Data Entry

. clear

. use nl_test

Stata Data in Stata Editor

Page 137: STATA White tutorial

Data Entry

. clear

. use nl_test

Stata Data in Stata Editor

Page 138: STATA White tutorial

Data Entry

. clear

. use nl_test

Stata Data in Stata Editor

Page 139: STATA White tutorial

Data Entry

. clear

. use nl_test

Stata Data in Stata Editor

Page 140: STATA White tutorial

Data Entry

. clear

. use nl_test

Stata Data in Stata Editor

Page 141: STATA White tutorial

Data EntryStata Data in Stata Editor

Save

Page 142: STATA White tutorial

Data EntryStata Data in Stata Editor

Undo changes since last “save”

Page 143: STATA White tutorial

Data EntryStata Data in Stata Editor

Sort by selected column

Page 144: STATA White tutorial

Data EntryStata Data in Stata Editor

Move selected column to the end

Page 145: STATA White tutorial

Data EntryStata Data in Stata Editor

Hide selected column

Page 146: STATA White tutorial

Data EntryStata Data in Stata Editor

Delete selected columns or rows

Page 147: STATA White tutorial

Data EntryStata Data in Stata Editor

Change selected value

Page 148: STATA White tutorial

Data Entry

. clear

. use nl_test

Exporting Stata Files to other Extensions

Page 149: STATA White tutorial

Data Entry

. clear

. use nl_test

. outsheet v1 v2 using nl_test.csv , comma

Exporting Stata Files to other Extensions

Probably the easiest way of exporting stata files to other extensions is to convert the Stata .dta file to a comma delimited file and then that file can be read by Excel, SPSS and other programs. We use the comma option to indicate we want a comma delimited file.

Page 150: STATA White tutorial

Data Entry

. clear

. use nl_test

. outsheet v1 v2 using nl_test.csv , comma

. type nl_test.csv

v1,v2.038,4.5.053,14.5.077,24.5.1,34.5.167,44.5.25,54.5.5,105

Exporting Stata Files to other Extensions

type command shows how the file nl_test.csvlooks like!

Page 151: STATA White tutorial

Knowing about the Data set

Stata provides a number of commands to facilitate our ability to 'get to know' the dataset we are using. These commands range from

• listing of the variables to • listing of the names, • labels, • frequency distributions, and • summary information

on any or every variable.

Page 152: STATA White tutorial

Knowing about the Data set1. Describe:

The describe command lists the contents of a dataset:. clear. use nl_test

Page 153: STATA White tutorial

Knowing about the Data set1. Describe:

The describe command lists the contents of a dataset:. clear. use nl_test. describe

Page 154: STATA White tutorial

Knowing about the Data set1. Describe:

The describe command lists the contents of a dataset:. clear. use nl_test. describe

Contains dataobs: 7 vars: 2 size: 84 (99.9% of memory free)

---------------------------------------------------------------------storage display value

variable name type format label variable label---------------------------------------------------------------------v1 float %9.0g v2 float %9.0g ---------------------------------------------------------------------Sorted by:

Page 155: STATA White tutorial

Knowing about the Data set1. Describe:

The describe command lists the contents of a dataset:. clear. use nl_test. describe

The describe command provides information on the • number of observations, • the number of variables, and • the size of the dataset. • Describe also lists the names of the variables, their type

and display format, along with any labels that may be included in the dataset.

Page 156: STATA White tutorial

Knowing about the Data set2. Code Book:

. clear

. use nl_test

. codebook

Page 157: STATA White tutorial

Knowing about the Data set2. Code Book:

. clear

. use nl_test

. codebook

v1 ---------------------------------------------- (unlabeled)type: numeric (float)

range: [.038,.5] units: .001unique values: 7 coded missing: 0 / 7

tabulation: Freq. Value1 .0381 .0531 .0771 .11 .1671 .251 .5

v2 ----------------------------------------------- (unlabeled)type: numeric (float)

range: [4.5,105] units: .1unique values: 7 coded missing: 0 / 7

tabulation: Freq. Value1 4.51 14.51 24.51 34.51 44.51 54.51 105

Page 158: STATA White tutorial

Knowing about the Data set2. Code Book:

. clear

. use nl_test

. codebook

The codebook command examines the • variable names, • labels, and • datato produce a codebook describing the data. Codebook will

provide summary information for variables Stata thinks are continuous (mean, standard deviation, percentiles).

Page 159: STATA White tutorial

Knowing about the Data set3. Summarize:

. clear

. use nl_test

Page 160: STATA White tutorial

Knowing about the Data set3. Summarize:

. clear

. use nl_test

. summarize

Page 161: STATA White tutorial

Knowing about the Data set3. Summarize:

. clear

. use nl_test

. summarize

Variable | Obs Mean Std. Dev. Min Max-------------+-----------------------------------------------------

v1 | 7 .1692857 .1632827 .038 .5v2 | 7 40.28571 33.2564 4.5 105

Page 162: STATA White tutorial

Knowing about the Data set3. Summarize:

. clear

. use nl_test

. summarize

The summarize command presents univariate statistics for • all (if no varlist is specified) or • particular (if a varlist is specified) variables. This summary includes the number of valid observations,

mean, standard deviation, minimum, and maximum valuefor each variable included in the command.

Page 163: STATA White tutorial

Knowing about the Data set4. List:

. clear

. use nl_test

. list

To look directly at the values that a variable contains, we can use the command list followed by the name of the variable.

Page 164: STATA White tutorial

Knowing about the Data set4. List:

. clear

. use nl_test

. listv1 v2

1. .038 4.5 2. .053 14.5 3. .077 24.5 4. .1 34.5 5. .167 44.5 6. .25 54.5 7. .5 105

Page 165: STATA White tutorial

Knowing about the Data set4. List:

. clear

. use nl_test

. list v1v1

1. .038 2. .053 3. .077 4. .1 5. .167 6. .25 7. .5

Page 166: STATA White tutorial

Knowing about the Data set4. List:

. clear

. use nl_test

. list v1 in 3/6v1

3. .077 4. .1 5. .167 6. .25

Page 167: STATA White tutorial

Knowing about the Data set4. List:

. clear

. use nl_test

. list if (v2 >= 40)v1 v2

5. .167 44.5 6. .25 54.5 7. .5 105

Page 168: STATA White tutorial

Knowing about the Data set4. List:

. clear

. use nl_test

. list if (v2 >= 40) & (v1 >= .2)v1 v2

6. .25 54.5 7. .5 105

Page 169: STATA White tutorial

Knowing about the Data set5. Inspect:

. clear

. use nl_test

Page 170: STATA White tutorial

Knowing about the Data set5. Inspect:

. clear

. use nl_test

. inspect

Page 171: STATA White tutorial

Knowing about the Data set5. Inspect:

. clear

. use nl_test

. Inspect

v1: Number of Observations----- Non-

Total Integers Integers| # Negative - - -| # Zero - - -| # Positive 7 - 7| # ----- ----- -----| # Total 7 - 7| # # # . # Missing -+---------------------- -----.038 .5 7

(7 unique values)

v2: Number of Observations----- Non-

Total Integers Integers| # Negative - - -| # Zero - - -| # # Positive 7 1 6| # # ----- ----- -----| # # # # Total 7 1 6| # # # . # Missing -+---------------------- -----4.5 105 7

(7 unique values)

Page 172: STATA White tutorial

Knowing about the Data set5. Inspect:

. clear

. use nl_test

. Inspect

inspect command is useful command for getting a quick overview of a data file.

Page 173: STATA White tutorial

Summary of Knowing about the Data Set:Key Stata Commands:

. describe

. codebook

. summarize

. list

. inspect

Knowing about the Data set

Page 174: STATA White tutorial

• Variables• Time Variables

4. Stata VariableStata Variable

Page 175: STATA White tutorial

Like other statistical packages, stata has two sort of variables

• string• numeric

each variables are associated with a label and format.

VariablesVariable Types

Page 176: STATA White tutorial

For the data nl_test.dta, we type:

. clear

. use nl_test

VariablesRenaming Variables

Page 177: STATA White tutorial

For the data nl_test.dta, we type:

. clear

. use nl_test

. rename v1 x

VariablesRenaming Variables

Page 178: STATA White tutorial

For the data nl_test.dta, we type:

. clear

. use nl_test

. rename v1 x

. rename v2 y

VariablesRenaming Variables

Page 179: STATA White tutorial

For the data nl_test.dta, we type:

. clear

. use nl_test

. rename v1 x

. rename v2 y

by these, we rename v1 as x and v2 as yrespectively.

VariablesRenaming Variables

Page 180: STATA White tutorial

For the data nl_test.dta, we type:

. clear

. use nl_test

. rename v1 x

. rename v2 y

VariablesLabeling Variables

Page 181: STATA White tutorial

For the data nl_test.dta, we type:

. clear

. use nl_test

. rename v1 x

. rename v2 y

. label variable x "X-label"

VariablesLabeling Variables

Page 182: STATA White tutorial

For the data nl_test.dta, we type:

. clear

. use nl_test

. rename v1 x

. rename v2 y

. label variable x "X-label"

. label variable y "Y-label“

VariablesLabeling Variables

Page 183: STATA White tutorial

For the data nl_test.dta, we type:

. clear

. use nl_test

. rename v1 x

. rename v2 y

. label variable x "X-label"

. label variable y "Y-label“

by these, we label x as “X-label” and y as “Y-label” respectively.

VariablesLabeling Variables

Page 184: STATA White tutorial

For the data nl_test.dta, we type:

. clear

. use nl_test

. rename v1 x

. rename v2 y

. label variable x "X-label"

. label variable y "Y-label“

. format x %5.3g

VariablesFormatting Variables

Page 185: STATA White tutorial

For the data nl_test.dta, we type:

. clear

. use nl_test

. rename v1 x

. rename v2 y

. label variable x "X-label"

. label variable y "Y-label“

. format x %5.3g

VariablesFormatting Variables

. list xx

1. .038 2. .053 3. .077 4. .100 5. .167 6. .250 7. .500

Page 186: STATA White tutorial

For the data nl_test.dta, we type:

. clear

. use nl_test

. rename v1 x

. rename v2 y

. label variable x "X-label"

. label variable y "Y-label“

. format x %5.3g

. format y %6.2g

VariablesFormatting Variables

Page 187: STATA White tutorial

For the data nl_test.dta, we type:

. clear

. use nl_test

. rename v1 x

. rename v2 y

. label variable x "X-label"

. label variable y "Y-label“

. format x %5.3g

. format y %6.2g

VariablesFormatting Variables

. list yy

1. 4.50 2. 14.50 3. 24.50 4. 34.50 5. 44.50 6. 54.50 7. 105.0

Page 188: STATA White tutorial

For the data nl_test.dta, we type:

. clear

. use nl_test

. rename v1 x

. rename v2 y

. label variable x "X-label"

. label variable y "Y-label“

. format x %5.3g

. format y %6.2g

by these, we set format for x and y respectively for 3 and 2 decimal places.

VariablesFormatting Variables

. list yy

1. 4.50 2. 14.50 3. 24.50 4. 34.50 5. 44.50 6. 54.50 7. 105.0

Page 189: STATA White tutorial

For the data nl_test.dta, we just entered the following commands one by one:

. clear

. use nl_test

. rename v1 x

. rename v2 y

. label variable x "X-label"

. label variable y "Y-label“

. format x %5.3g

. format y %6.2g

VariablesDoing the entire procedure by Do-Files

Page 190: STATA White tutorial

For the data nl_test.dta, we just entered the following commands one by one:

. clear

. use nl_test

. rename v1 x

. rename v2 y

. label variable x "X-label"

. label variable y "Y-label“

. format x %5.3g

. format y %6.2g

VariablesDoing the entire procedure by Do-Files

to carry out a batch of commands for a particular analysis, it is sometimes helpful to make a file that contains all the necessary commands, which can be saved for future statasessions.

Page 191: STATA White tutorial

Such files can be made up by Stata do-file editor or any other text file editor. To enter in Stata do-file editor, we have to do as follows:

VariablesDoing the entire procedure by Do-Files

Page 192: STATA White tutorial

Such files can be made up by Stata do-file editor or any other text file editor. To enter in Stata do-file editor, we have to do as follows:

VariablesDoing the entire procedure by Do-Files

Page 193: STATA White tutorial

Such files can be made up by Stata do-file editor or any other text file editor. To enter in Stata do-file editor, we have to do as follows:

VariablesDoing the entire procedure by Do-Files

or just type . doeditin the statacommands to do as described here.

Page 194: STATA White tutorial

Such files can be made up by Stata do-file editor or any other text file editor. To enter in Stata do-file editor, we have to do as follows:

VariablesDoing the entire procedure by Do-Files

or we can also do it from the stata toolbox to do the same

Page 195: STATA White tutorial

Such files can be made up by Stata do-file editor or any other text file editor. To enter in Stata do-file editor, we have to do as follows:

VariablesDoing the entire procedure by Do-Files

or we can also do it from the stata toolbox to do the same

Page 196: STATA White tutorial

For the data nl_test.raw, we can enter the following commands as a batch in the Stata do-file editor :

infile v1 v2 using nl_test.rawrename v1 xrename v2 ylabel variable x "X-label"label variable y "Y-label“format x %5.3gformat y %6.2g

VariablesDoing the entire procedure by Do-Files

Page 197: STATA White tutorial

For the data nl_test.raw, we can enter the following commands as a batch in the Stata do-file editor as:

VariablesDoing the entire procedure by Do-Files

Page 198: STATA White tutorial

save it as rawdataread stata do-file and then run current file:

VariablesDoing the entire procedure by Do-Files

Page 199: STATA White tutorial

save it as rawdataread stata do-file and then run current file:

VariablesDoing the entire procedure by Do-Files

Page 200: STATA White tutorial

or, we could save the file and clear the memory, or open another session, and do as follows:

VariablesDoing the entire procedure by Do-Files

Page 201: STATA White tutorial

or, we could save the file and clear the memory, or open another session, and do as follows:

VariablesDoing the entire procedure by Do-Files

Page 202: STATA White tutorial

VariablesDoing the entire procedure by Do-Files

Page 203: STATA White tutorial

Or we could write . do rawdataread

to run the batch just as the same.

VariablesDoing the entire procedure by Do-Files

Page 204: STATA White tutorial

Therefore, the Do-file Editor lets you submit several commands to Stata at once. If there is an error in some command, Stata stops the implementation of the do-file and reports the error.

On the other hand, Ado is for perfect programs that can be run repeatedly. Built-in commands such as regress and tabulate are implemented as .ado files.

VariablesDoing the entire procedure by Do-Files

Page 205: STATA White tutorial

For example, let us input the marriage dummy variabledata as follows:

. clear

. input marriage43431342233

VariablesLabelling Variable Values

2213333132end

Page 206: STATA White tutorial

We then define its value labels as:

. label define marriage 1 married 2 single 3 divorced 4 other

VariablesLabelling Variable Values

Page 207: STATA White tutorial

We then define its value labels as:

. label define marriage 1 married 2 single 3 divorced 4 other

. label list

VariablesLabelling Variable Values

Page 208: STATA White tutorial

We then define its value labels as:

. label define marriage 1 married 2 single 3 divorced 4 other

. label listmarriage:

1 married2 single3 divorced4 other

VariablesLabelling Variable Values

Page 209: STATA White tutorial

. recode marriage min/2=1 3/max=2

VariablesRecoding Variable Values

Page 210: STATA White tutorial

. recode marriage min/2=1 3/max=2

Changes / converts 1st to 2nd category to category 1and converts 3rd to 4th category to category 2.

VariablesRecoding Variable Values

Page 211: STATA White tutorial

. listmarriage

1. 2 2. 2 3. 2 4. 2 5. 1 6. 2 7. 2 8. 1 9. 1 10. 2 11. 2 12. 1 13. 1 14. 1 15. 2 16. 2 17. 2 18. 2 19. 1 20. 2 21. 1

VariablesRecoding Variable Values

Page 212: STATA White tutorial

. listmarriage

1. 2 2. 2 3. 2 4. 2 5. 1 6. 2 7. 2 8. 1 9. 1 10. 2 11. 2 12. 1 13. 1 14. 1 15. 2 16. 2 17. 2 18. 2 19. 1 20. 2 21. 1

VariablesRecoding Variable Values

Converted from the original values

Page 213: STATA White tutorial

. listmarriage

1. 2 2. 2 3. 2 4. 2 5. 1 6. 2 7. 2 8. 1 9. 1 10. 2 11. 2 12. 1 13. 1 14. 1 15. 2 16. 2 17. 2 18. 2 19. 1 20. 2 21. 1

VariablesRecoding Variable Values

434313422332213333132

Converted from the original values

Page 214: STATA White tutorial

Like S-plus / Matlab, each observation has an index associated with it.

VariablesIndex of the Observations

Page 215: STATA White tutorial

Like S-plus / Matlab, each observation has an index associated with it.

For the marriage dummy variable recodeddata,

VariablesIndex of the Observations

. listmarriage

1. 2 2. 2 3. 2 4. 2 5. 1 6. 2 7. 2 8. 1 9. 1 10. 2 11. 2 12. 1 13. 1 14. 1 15. 2 16. 2 17. 2 18. 2 19. 1 20. 2 21. 1

Page 216: STATA White tutorial

Like S-plus / Matlab, each observation has an index associated with it.

For the marriage dummy variable recodeddata, the 5th observation

VariablesIndex of the Observations

. listmarriage

1. 2 2. 2 3. 2 4. 2 5. 16. 2 7. 2 8. 1 9. 1 10. 2 11. 2 12. 1 13. 1 14. 1 15. 2 16. 2 17. 2 18. 2 19. 1 20. 2 21. 1

Page 217: STATA White tutorial

Like S-plus / Matlab, each observation has an index associated with it.

For the marriage dummy variable recodeddata, the 5th observation can be extracted as follows:

VariablesIndex of the Observations

. listmarriage

1. 2 2. 2 3. 2 4. 2 5. 16. 2 7. 2 8. 1 9. 1 10. 2 11. 2 12. 1 13. 1 14. 1 15. 2 16. 2 17. 2 18. 2 19. 1 20. 2 21. 1

Page 218: STATA White tutorial

Like S-plus / Matlab, each observation has an index associated with it.

For the marriage dummy variable recodeddata, the 5th observation can be extracted as follows:

. display marriage[5]

VariablesIndex of the Observations

. listmarriage

1. 2 2. 2 3. 2 4. 2 5. 16. 2 7. 2 8. 1 9. 1 10. 2 11. 2 12. 1 13. 1 14. 1 15. 2 16. 2 17. 2 18. 2 19. 1 20. 2 21. 1

Page 219: STATA White tutorial

Like S-plus / Matlab, each observation has an index associated with it.

For the marriage dummy variable recodeddata, the 5th observation can be extracted as follows:

. display marriage[5]1

VariablesIndex of the Observations

. listmarriage

1. 2 2. 2 3. 2 4. 2 5. 16. 2 7. 2 8. 1 9. 1 10. 2 11. 2 12. 1 13. 1 14. 1 15. 2 16. 2 17. 2 18. 2 19. 1 20. 2 21. 1

Page 220: STATA White tutorial

Like S-plus / Matlab, each observation has an index associated with it.

For the marriage dummy variable recodeddata, the number of observations can be extracted as follows:

. display _N

VariablesIndex of the Observations

. listmarriage

1. 2 2. 2 3. 2 4. 2 5. 1 6. 2 7. 2 8. 1 9. 1 10. 2 11. 2 12. 1 13. 1 14. 1 15. 2 16. 2 17. 2 18. 2 19. 1 20. 2 21. 1

Page 221: STATA White tutorial

Like S-plus / Matlab, each observation has an index associated with it.

For the marriage dummy variable recodeddata, the number of observations can be extracted as follows:

. display _N21

VariablesIndex of the Observations

. listmarriage

1. 2 2. 2 3. 2 4. 2 5. 1 6. 2 7. 2 8. 1 9. 1 10. 2 11. 2 12. 1 13. 1 14. 1 15. 2 16. 2 17. 2 18. 2 19. 1 20. 2 21. 1

Page 222: STATA White tutorial

To change any particular value of the variable, we use replacecommand indicating particular value or observation as _n. For example, for the marriage dummy variable recoded data:

VariablesChanging Value of variable :The if qualifier

. listmarriage

1. 2 2. 2 3. 2 4. 2 5. 1 6. 2 7. 2 8. 1 9. 1 10. 2 11. 2 12. 1 13. 1 14. 1 15. 2 16. 2 17. 2 18. 2 19. 1 20. 2

21. 1

Page 223: STATA White tutorial

To change any particular value of the variable, we use replacecommand indicating particular value or observation as _n. For example, for the marriage dummy variable recoded data:

. replace marriage = . if _n==21(1 real change made, 1 to missing)

VariablesChanging Value of variable :The if qualifier

. listmarriage

1. 2 2. 2 3. 2 4. 2 5. 1 6. 2 7. 2 8. 1 9. 1 10. 2 11. 2 12. 1 13. 1 14. 1 15. 2 16. 2 17. 2 18. 2 19. 1 20. 2

21. .

Page 224: STATA White tutorial

To change any particular value of the variable, we use replacecommand indicating particular value, for the marriage dummy variablerecoded data:

. replace marriage = . in 21

VariablesChanging Value of variable :The in qualifier

. listmarriage

1. 2 2. 2 3. 2 4. 2 5. 1 6. 2 7. 2 8. 1 9. 1 10. 2 11. 2 12. 1 13. 1 14. 1 15. 2 16. 2 17. 2 18. 2 19. 1 20. 2

21. .

Page 225: STATA White tutorial

Note that the system missing numerical value is denoted by a

period (.), or a period followed by any letter.

In stata, missing values are saved as a very large positive number.

In the example, 21th observationis noted as a missing value.

VariablesMissing Values in Variables

. listmarriage

1. 2 2. 2 3. 2 4. 2 5. 1 6. 2 7. 2 8. 1 9. 1 10. 2 11. 2 12. 1 13. 1 14. 1 15. 2 16. 2 17. 2 18. 2 19. 1 20. 2

21. .

Page 226: STATA White tutorial

. list marriage in f/lmarriage

1. 2 2. 2 3. 2 4. 2 5. 1 6. 2 7. 2 8. 1 9. 1 10. 2 11. 2 12. 1 13. 1 14. 1 15. 2 16. 2 17. 2 18. 2 19. 1 20. 2 21. .

VariablesPartitioning variable list output

. listmarriage

1. 2 2. 2 3. 2 4. 2 5. 1 6. 2 7. 2 8. 1 9. 1

10. 2 11. 2 12. 1 13. 1 14. 1 15. 2 16. 2 17. 2 18. 2 19. 1 20. 2 21. .

Page 227: STATA White tutorial

. list marriage in f/10marriage

1. 2 2. 2 3. 2 4. 2 5. 1 6. 2 7. 2 8. 1 9. 1 10. 2

VariablesPartitioning variable list output

. list marriage in f/lmarriage

1. 2 2. 2 3. 2 4. 2 5. 1 6. 2 7. 2 8. 1 9. 1

10. 2 11. 2 12. 1 13. 1 14. 1 15. 2 16. 2 17. 2 18. 2 19. 1 20. 2 21. .

Page 228: STATA White tutorial

. list marriage in -11/lmarriage

11. 2 12. 1 13. 1 14. 1 15. 2 16. 2 17. 2 18. 2 19. 1 20. 2 21. .

VariablesPartitioning variable list output

. list marriage in f/lmarriage

1. 2 2. 2 3. 2 4. 2 5. 1 6. 2 7. 2 8. 1 9. 1

10. 2 11. 2 12. 1 13. 1 14. 1 15. 2 16. 2 17. 2 18. 2 19. 1 20. 2 21. .

Page 229: STATA White tutorial

. sort marriage

VariablesSorting variable

. list marriage in f/lmarriage

1. 2 2. 2 3. 2 4. 2 5. 1 6. 2 7. 2 8. 1 9. 1

10. 2 11. 2 12. 1 13. 1 14. 1 15. 2 16. 2 17. 2 18. 2 19. 1 20. 2 21. .

Page 230: STATA White tutorial

. sort marriage

. listmarriage

1. 1 2. 1 3. 1 4. 1 5. 1 6. 1 7. 1 8. 2 9. 2 10. 2 11. 2 12. 2 13. 2 14. 2 15. 2 16. 2 17. 2 18. 2 19. 2 20. 2 21. .

VariablesSorting variable

. list marriage in f/lmarriage

1. 2 2. 2 3. 2 4. 2 5. 1 6. 2 7. 2 8. 1 9. 1

10. 2 11. 2 12. 1 13. 1 14. 1 15. 2 16. 2 17. 2 18. 2 19. 1 20. 2 21. .

Page 231: STATA White tutorial

. sort marriage

. listmarriage

1. 1 2. 1 3. 1 4. 1 5. 1 6. 1 7. 1 8. 2 9. 2 10. 2 11. 2 12. 2 13. 2 14. 2 15. 2 16. 2 17. 2 18. 2 19. 2 20. 2 21. .

VariablesSorting variable

. list marriage in f/lmarriage

1. 2 2. 2 3. 2 4. 2 5. 1 6. 2 7. 2 8. 1 9. 1

10. 2 11. 2 12. 1 13. 1 14. 1 15. 2 16. 2 17. 2 18. 2 19. 1 20. 2 21. .

Sorted from the original values -

Page 232: STATA White tutorial

. drop if marriage==.(1 observation deleted)

VariablesEditing (dropping) Variable Values

. list marriage in f/lmarriage

1. 1 2. 1 3. 1 4. 1 5. 1 6. 1 7. 1 8. 2 9. 2

10. 2 11. 2 12. 2 13. 2 14. 2 15. 2 16. 2 17. 2 18. 2 19. 2 20. 2 21. .

Page 233: STATA White tutorial

. drop if marriage==.(1 observation deleted). list

marriage 1. 1 2. 1 3. 1 4. 1 5. 1 6. 1 7. 1 8. 2 9. 2 10. 2 11. 2 12. 2 13. 2 14. 2 15. 2 16. 2 17. 2 18. 2 19. 2 20. 2

VariablesEditing (dropping) Variable Values

. list marriage in f/lmarriage

1. 1 2. 1 3. 1 4. 1 5. 1 6. 1 7. 1 8. 2 9. 2

10. 2 11. 2 12. 2 13. 2 14. 2 15. 2 16. 2 17. 2 18. 2 19. 2 20. 2 21. .

Page 234: STATA White tutorial

. keep if marriage>=2(3 observations deleted)

VariablesEditing (keeping) Variable Values

. list marriage in f/lmarriage

1. 1 2. 1 3. 1 4. 1 5. 1 6. 1 7. 1 8. 2 9. 2

10. 2 11. 2 12. 2 13. 2 14. 2 15. 2 16. 2 17. 2 18. 2 19. 2 20. 2

Page 235: STATA White tutorial

. keep if marriage>=2(3 observations deleted). list

marriage 1. 2 2. 2 3. 2 4. 2 5. 2 6. 2 7. 2 8. 2 9. 2 10. 2 11. 2 12. 2 13. 2

VariablesEditing (keeping) Variable Values

. list marriage in f/lmarriage

1. 1 2. 1 3. 1 4. 1 5. 1 6. 1 7. 1 8. 2 9. 2

10. 2 11. 2 12. 2 13. 2 14. 2 15. 2 16. 2 17. 2 18. 2 19. 2 20. 2

Page 236: STATA White tutorial

VariablesGenerating New Variables

> Greater than< Less than>= Greater or equal = = Equal~ = Not equal

| Or~ Not & And

+ Addition- Subtraction* Multiplication/ Division^ Power

Relational operatorsLogical operatorsArithmetic operators

We can easily generate new variables by using these simple mathematical symbols and expressions.

Page 237: STATA White tutorial

VariablesGenerating New Variables

We can easily generate new variables by using these simple mathematical symbols and expressions.

computes the square root of xsqrt(x)computes the log base 10 of xlog10(x)computes the natural logarithm of xLn(x) or log(x)provides the exponentiation of xexp(x)computes the absolute value of xabs(x)

Mathematical Functions

Page 238: STATA White tutorial

VariablesGenerating New Variables

We can easily generate new variables by using these simple mathematical symbols and expressions.

The most common way of creating new variables is Stata’s command generate

Page 239: STATA White tutorial

VariablesGenerating New Variables

We can easily generate new variables by using these simple mathematical symbols and expressions.

The most common way of creating new variables is Stata’s command generate

. use "C:\DATA\hsb2.dta", clear(highschool and beyond (200 cases))This data contains variables id, female, race, ses, schtyp, prog, read, write, math, science, socst.

We make another variable score by typing:. generate score = read + write + math + science + socst

Page 240: STATA White tutorial

VariablesGenerating New Variables

We can easily generate new variables by using these simple mathematical symbols and expressions.

If we want to generate a subset of the variable math, let us call it mathpass, for which, the numbers are countable if the math score is above 60

Page 241: STATA White tutorial

VariablesGenerating New Variables

We can easily generate new variables by using these simple mathematical symbols and expressions.

If we want to generate a subset of the variable math, let us call it mathpass, for which, the numbers are countable if the math score is above 60

. gen mathpass = math if math>60

Page 242: STATA White tutorial

VariablesGenerating New Variables

We can easily generate new variables by using these simple mathematical symbols and expressions.

If we want to generate a subset of the variable math, let us call it mathpass, for which, the numbers are countable if the math score is above 60

. gen mathpass = math if math>60

. list mathpass

this shows all the math scores above 60 number, converting the rest of the numbers into missing values.

Page 243: STATA White tutorial

VariablesGenerating New Variables

Also egen creates new variable of the optionally specified storage type equal to fcn(arguments).

. use hs0

Page 244: STATA White tutorial

VariablesGenerating New Variables

Also egen creates new variable of the optionally specified storage type equal to fcn(arguments).

. use hs0

. describeContains data from hs0.dta

obs: 200 vars: 14 20 Jun 2002 12:42size: 12,800 (97.9% of memory free)

---------------------------------------------------------------------storage display value

variable name type format label variable label---------------------------------------------------------------------gender float %9.0g fl id float %9.0g race float %12.0g rlses float %9.0g slschtyp float %9.0g sclprgtype str8 %9s read float %9.0g reading scorewrite float %9.0g writing scoremath float %9.0g math scorescience float %9.0g science scoresocst float %9.0g social studies scoreprog long %8.0g prognewvar1 float %9.0g newvar2 float %9.0g ---------------------------------------------------------------------Sorted by:

Page 245: STATA White tutorial

VariablesGenerating New Variables

Also egen creates new variable of the optionally specified storage type equal to fcn(arguments).

. use hs0

. describe

. egen newvar0 = rank(math) rank(exp) creates ranks (within varlist) of exp; by default, equal observations are assigned the average rank.

Page 246: STATA White tutorial

VariablesGenerating New Variables

Also egen creates new variable of the optionally specified storage type equal to fcn(arguments).

. use hs0

. describe

. egen newvar0 = rank(math)

. egen newvar1 = count(math)count(exp) creates a constant (within varlist) containing the number of nonmissing observations of exp.

Page 247: STATA White tutorial

VariablesGenerating New Variables

Also egen creates new variable of the optionally specified storage type equal to fcn(arguments).

. use hs0

. describe

. egen newvar0 = rank(math)

. egen newvar1 = count(math)

. egen newvar2 = kurt(math) kurt(varname) returns the kurtosis of varname.

Page 248: STATA White tutorial

VariablesGenerating New Variables

Also egen creates new variable of the optionally specified storage type equal to fcn(arguments).

. use hs0

. describe

. egen newvar0 = rank(math)

. egen newvar1 = count(math)

. egen newvar2 = kurt(math)

. egen newvar3 = max(math) max(exp) creates a constant (within varlist) containing the maximum value of exp.

Page 249: STATA White tutorial

For merging 2 data sets with same variables , such as ans1 and ans2 stata data sets, we use mergecommand. Let us demonstrate how:

VariablesMerging data files with same variables

Page 250: STATA White tutorial

For merging 2 data sets with same variables , such as ans1 and ans2 stata data sets, we use mergecommand.

VariablesMerging data files with same variables

. clear

. use ans1

. list

y ans1hat x 1. 8.04 8.001 10 2. 6.95 7.000818 8 3. 7.58 9.501273 13 4. 8.81 7.500909 9 5. 8.33 8.501091 11 6. 9.96 10.00136 14 7. 7.24 6.000637 6 8. 4.26 5.000454 4 9. 10.84 9.001182 12 10. 4.82 6.500727 7 11. 5.68 5.500546 5

Page 251: STATA White tutorial

For merging 2 data sets with same variables , such as ans1 and ans2 stata data sets, we use mergecommand.

VariablesMerging data files with same variables

. clear

. use ans1

. list

y ans1hat x 1. 8.04 8.001 10 2. 6.95 7.000818 8 3. 7.58 9.501273 13 4. 8.81 7.500909 9 5. 8.33 8.501091 11 6. 9.96 10.00136 14 7. 7.24 6.000637 6 8. 4.26 5.000454 4 9. 10.84 9.001182 12 10. 4.82 6.500727 7 11. 5.68 5.500546 5

. clear

. use ans2

. list

y ans2hat x 1. 9.14 8.000909 10 2. 8.14 7.000909 8 3. 8.74 9.500909 13 4. 8.77 7.500909 9 5. 9.26 8.500909 11 6. 8.1 10.00091 14 7. 6.13 6.000909 6 8. 3.1 5.000909 4 9. 9.13 9.000909 12 10. 7.26 6.500909 7 11. 4.74 5.500909 5

Page 252: STATA White tutorial

For merging 2 data sets with same variables , such as ans1 and ans2 stata data sets, we use mergecommand.

VariablesMerging data files with same variables

. clear

. use ans1

. merge using ans2

. list

Page 253: STATA White tutorial

For merging 2 data sets with same variables , such as ans1 and ans2 stata data sets, we use mergecommand.

VariablesMerging data files with same variables

. clear

. use ans1

. merge using ans2

. listy ans1hat x ans2hat _merge

1. 8.04 8.001 10 8.000909 3 2. 6.95 7.000818 8 7.000909 3 3. 7.58 9.501273 13 9.500909 3 4. 8.81 7.500909 9 7.500909 3 5. 8.33 8.501091 11 8.500909 3 6. 9.96 10.00136 14 10.00091 3 7. 7.24 6.000637 6 6.000909 3 8. 4.26 5.000454 4 5.000909 3 9. 10.84 9.001182 12 9.000909 3 10. 4.82 6.500727 7 6.500909 3 11. 5.68 5.500546 5 5.500909 3

Page 254: STATA White tutorial

VariablesSummary of Variables Manipulation:Key Stata Commands:

rename [to rename an existing variable]label variable [to put label on variable]format [to dictate the format of a variable]label define [to put labels on values]recode [to recode variable values]sort [to sort variable values]drop [to drop variable values]keep [to keep variable values]generate [to generate new variable]merge [to merge two data set]The if qualifier, The in qualifierNumber of observations = _NMissing Values = period (.)

Page 255: STATA White tutorial

Time VariablesFormat for Time variables

Let us consider the following example:

. clear

. input time

. 15673

. 10345

. 7654

. 3564

. 1023

. 0

. end

Page 256: STATA White tutorial

Time VariablesFormat for Time variables

Let us consider the following example:

. clear

. input time

. 15673

. 10345

. 7654

. 3564

. 1023

. 0

. end

. format time %d

Page 257: STATA White tutorial

Time VariablesFormat for Time variables

Let us consider the following example:

. clear

. input time

. 15673

. 10345

. 7654

. 3564

. 1023

. 0

. end

. format time %d

. list timetime 1. 29nov2002 2. 28apr1988 3. 15dec1980 4. 04oct1969 5. 20oct1962 6. 01jan1960

Page 258: STATA White tutorial

Time VariablesFormat for Time variables

Let us consider the following example:

. clear

. input time

. 15673

. 10345

. 7654

. 3564

. 1023

. 0

. end

. format time %d

. list timetime 1. 29nov2002 2. 28apr1988 3. 15dec1980 4. 04oct1969 5. 20oct1962 6. 01jan1960

STATA counts time starting from 1st January of 1960.

Page 259: STATA White tutorial

Time VariablesFormat for Time variables

Let us consider the following example:

. clear

. input time

. 15673

. 10345

. 7654

. 3564

. 1023

. 0

. end

. format time %dM_d,_CY

. listtime

1. November 29, 2002 2. April 28, 1988 3. December 15, 1980 4. October 4, 1969 5. October 20, 1962 6. January 1, 1960

Page 260: STATA White tutorial

Time VariablesFormat for Time variables

Let us consider the following example:

. clear

. input time

. 15673

. 10345

. 7654

. 3564

. 1023

. 0

. end

. format time %dN/D/Y

. listtime

1. 11/29/02 2. 04/28/88 3. 12/15/80 4. 10/04/69 5. 10/20/62 6. 01/01/60

Page 261: STATA White tutorial

Time VariablesFormat for Time variables

Let us consider the following example:

. clear

. input time

. 15673

. 10345

. 7654

. 3564

. 1023

. 0

. end

. save datedata

Page 262: STATA White tutorial

Time VariablesFormat for Time variables

Let us consider the following example:

. clear

. input time

. 15673

. 10345

. 7654

. 3564

. 1023

. 0

. end

. save datedata

. generate mn=month(time)

. generate dy=day(time)

. generate yr=year(time)

Page 263: STATA White tutorial

Time VariablesFormat for Time variables

Let us consider the following example:

. clear

. input time

. 15673

. 10345

. 7654

. 3564

. 1023

. 0

. end

. save datedata

. generate mn=month(time)

. generate dy=day(time)

. generate yr=year(time)

. listtime mn dy yr

1. 11/29/02 11 29 2002 2. 04/28/88 4 28 1988 3. 12/15/80 12 15 1980 4. 10/04/69 10 4 1969 5. 10/20/62 10 20 1962 6. 01/01/60 1 1 1960

Page 264: STATA White tutorial

Time VariablesFormat for Time variables

Let us consider the following example:

. clear

. input time

. 15673

. 10345

. 7654

. 3564

. 1023

. 0

. end

. save datedata

. generate mn=month(time)

. generate dy=day(time)

. generate yr=year(time)

. listtime mn dy yr

1. 11/29/02 11 29 2002 2. 04/28/88 4 28 1988 3. 12/15/80 12 15 1980 4. 10/04/69 10 4 1969 5. 10/20/62 10 20 1962 6. 01/01/60 1 1 1960

. save datedata, replace

Page 265: STATA White tutorial

Summary of Time Variables:Key Stata Commands:

For time formats, the followings are the standards:

Format------------%d%dDlCY%dDlY%dM_d,_CY%dd_M_CY%dN/D/Y%dD/N/Y%dY/N/D%dN-D-CY

Time Variables

Page 266: STATA White tutorial

Summary of Time Variables:Key Stata Commands:

For time formats, the followings are the standards:

Format Sample date in format---------------------------%d 24dec1980 %dDlCY 24dec1980 %dDlY 24dec80 %dM_d,_CY December 24, 1980 %dd_M_CY 24 December 1980 %dN/D/Y 12/24/80 %dD/N/Y 24/12/80 %dY/N/D 80/12/14 %dN-D-CY 12-24-1980

Time Variables

Page 267: STATA White tutorial

• STATA system diagnosis

5. Stata SystemStata System

Page 268: STATA White tutorial

STATA system diagnosisChecking Status

Let us type in the command . describe or . din stata commands to get the output shown here:

Page 269: STATA White tutorial

STATA system diagnosisChecking Status

Let us type in the command . describe or . din stata commands to get the output shown here:

Page 270: STATA White tutorial

STATA system diagnosisChecking Status

Let us type in the command . describe or . din stata commands to get the output shown here:

which shows that no data is entered and the current memory status.

Page 271: STATA White tutorial

STATA system diagnosisDirectory Settings in Windows

We can check the present working directory by the following command

. pwdC:\DATA

Page 272: STATA White tutorial

STATA system diagnosis

We can check the contents of the present working directoryby the following command

. dir

Directory Settings in Windows

Page 273: STATA White tutorial

STATA system diagnosis

We can check the contents of the present working directoryby the following command

. dir

Directory Settings in Windows

Page 274: STATA White tutorial

STATA system diagnosis

The same thing can be done by typing

. ls

Directory Settings in Windows

Page 275: STATA White tutorial

STATA system diagnosis

The dir command gives the list of all files in that directory. But if we are to look for any stata data file, we just type in as. dir *.dta

Directory Settings in Windows

Page 276: STATA White tutorial

STATA system diagnosisDirectory Settings in Windows

To make directory store in c:\data\ we type. mkdir c:\data\store

Page 277: STATA White tutorial

STATA system diagnosisDirectory Settings in Windows

To make directory store in c:\data\ we type. mkdir c:\data\store

and to enter into that directory, we type. cd c:\data\storec:\data\store

Page 278: STATA White tutorial

STATA system diagnosisDirectory Settings in Windows

To make directory store in c:\data\ we type. mkdir c:\data\store

and to enter into that directory, we type. cd c:\data\storec:\data\store

To copy the file ans1.dta from c:\data\ (one level up directory of c:\data\store) to c:\data\store, then we type

. copy ..\ans1.dta ans1.dta

Page 279: STATA White tutorial

STATA system diagnosisKeeping Track of Work

It is advisable to keep track of all the commands and results in each session, in order to systemize the work.

For that purpose Stata allows you opening directly a log file -which is a text like file- that records all the executed commands and the results as they are displayed in the Stata Results window.

We can keep track of all what we are doing in a session by opening a log file at the beginning of the session and close this at its end.

Page 280: STATA White tutorial

STATA system diagnosisKeeping Track of Work

The log command allows us to keep a full record of your Stata session by opening a log file. What we need to do is:

Page 281: STATA White tutorial

STATA system diagnosisKeeping Track of Work

The log command allows us to keep a full record of your Stata session by opening a log file. What we need to do is:

Page 282: STATA White tutorial

STATA system diagnosisKeeping Track of Work

The log command allows us to keep a full record of your Stata session by opening a log file. What we need to do is:

Page 283: STATA White tutorial

STATA system diagnosisKeeping Track of Work

The log command allows us to keep a full record of your Stata session by opening a log file. What we need to do is:

Page 284: STATA White tutorial

STATA system diagnosisKeeping Track of Work

To view the log, we do as follows:

Page 285: STATA White tutorial

STATA system diagnosisKeeping Track of Work

We can do the whole process by using the following syntax:

. log using C:\Data\logs.logIf we wish to temporarily stop logging (suspend log file) type. log offwhile when we want to resume:. log onIn order to stop logging and close the file :. log close

Page 286: STATA White tutorial

STATA system diagnosisInterrupting Stata Commands / Processes

when a period appears at the bottom of the stata result window, it is ready to take new command. Otherwise the stata is working on something. To interrupt the process, press ctrl+pause/Break

Page 287: STATA White tutorial

STATA system diagnosisInterrupting Stata Commands / Processes

when a period appears at the bottom of the stata result window, it is ready to take new command. Otherwise the stata is working on something. To interrupt the process, press ctrl+pause/Break or presss the break button on the tool bar

Page 288: STATA White tutorial

STATA system diagnosisInterrupting Stata Commands / Processes

when a period appears at the bottom of the stata result window, it is ready to take new command. Otherwise the stata is working on something. To interrupt the process, press ctrl+pause/Break or presss the break button on the tool bar

Page 289: STATA White tutorial

Summary of Stata system diagnosis:Key Stata Commands:

. describe

. pwd

. dir

. mkdir

. cd

. copy

. log

STATA system diagnosis

Page 290: STATA White tutorial

Summary of Stata system diagnosis:Key Stata Commands:

. describe [to check status of system]

. pwd [to check location of working directory]

. dir [to check the contents of working directory]

. mkdir [to make new directory]

. cd [to change working directory]

. copy [to copy objects]

. log [log file command]

STATA system diagnosis

Page 291: STATA White tutorial

STATASTATA:: The The WhiteWhite tutorialtutorial