STATA White tutorial

Post on 03-Feb-2022

10 views 0 download

Transcript of STATA White tutorial

STATASTATA:: The The WhiteWhite tutorialtutorial

This tutorial presentation is prepared by

Mohammad Ehsanul Karimehsan.karim@gmail.com

STATASTATA:: The The WhiteWhite tutorialtutorial

This tutorial presentation is prepared by

Mohammad Ehsanul Karimehsan.karim@gmail.com

STATASTATA:: The The WhiteWhite tutorialtutorial

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

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

1. IntroductionIntroduction

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

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.

Introduction to Stata

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

Introduction to Stata

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

Stata Forms

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

Stata Forms

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

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

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.

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.

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.

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).

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.

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.

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.

Stata Windows

• Stata contains 4 basic windows:

Stata Windows

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

Stata Windows

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

• Each of these windows can be moved and resized!

Stata Windows

Stata Windows

Stata Command window

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

Stata Windows

Stata Windows

Stata Results

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.

Stata Windows

Stata Windows

Review window

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.

Stata Windows

Stata Windows

Variables window

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.

Stata WindowsSummary

Stata Windows

Stata Command window

Summary

Stata Windows

Stata Command window

Stata Results

Summary

Stata Windows

Stata Command window

Stata Results

Review window

Summary

Stata Windows

Stata Command window

Stata ResultsVariables window

Review window

Summary

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.

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

2. Stata HelpStata Help

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)

STATA: Help, Search, Tutorials, Manuals, Website

2. STATA HELP

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

STATA: Help, Search, Tutorials, Manuals, Website

. help

STATA: Help, Search, Tutorials, Manuals, Website

. help

gives an overview of help in the Stata Results

Type in the Stata Command window

STATA: Help, Search, Tutorials, Manuals, Website

. help

STATA: Help, Search, Tutorials, Manuals, Website

. help contents

STATA: Help, Search, Tutorials, Manuals, Website

. help contents

gives many pages of commands in the Stata Results

Type in the Stata Command window

STATA: Help, Search, Tutorials, Manuals, Website

. help contents

STATA: Help, Search, Tutorials, Manuals, Website

. help regress

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

STATA: Help, Search, Tutorials, Manuals, Website

. help regress

STATA: Help, Search, Tutorials, Manuals, Website

3. STATA SEARCH

This can give more results than Stata help

STATA: Help, Search, Tutorials, Manuals, Website

. search statistics

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

STATA: Help, Search, Tutorials, Manuals, Website

. search statistics

STATA: Help, Search, Tutorials, Manuals, Website

. search simulation

STATA: Help, Search, Tutorials, Manuals, Website

. search simulation

gives information on simulation in the Stata Results

Type in the Stata Command window

STATA: Help, Search, Tutorials, Manuals, Website

. search simulation

STATA: Help, Search, Tutorials, Manuals, Website

Also, we can do this as follows

STATA: Help, Search, Tutorials, Manuals, Website

Also, we can do this as follows

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.

STATA: Help, Search, Tutorials, Manuals, Website

4. STATA TUTORIALS

Stata gives several tutorials that demonstrate various modules.

STATA: Help, Search, Tutorials, Manuals, Website

. tutorial contents

STATA: Help, Search, Tutorials, Manuals, Website

. tutorial contents

lists available of turorials in the Stata Results

Type in the Stata Command window

STATA: Help, Search, Tutorials, Manuals, Website

. tutorial contents

STATA: Help, Search, Tutorials, Manuals, Website

. tutorial survival

STATA: Help, Search, Tutorials, Manuals, Website

. tutorial survival

demonstrates survival commands in the Stata Results

Type in the Stata Command window

STATA: Help, Search, Tutorials, Manuals, Website

. tutorial survival

STATA: Help, Search, Tutorials, Manuals, Website

5. STATA WEB-SITE

The website adderss is

http://www.stata.com/

STATA: Help, Search, Tutorials, Manuals, Website

5. STATA WEB-SITE

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

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).

STATA: Help, Search, Tutorials, Manuals, Website

searching through internet using stata

STATA: Help, Search, Tutorials, Manuals, Website

searching through internet using stata

STATA: Help, Search, Tutorials, Manuals, Website

searching through internet using stata

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.

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

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

STATA: Help, Search, Tutorials, Manuals, Website

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

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.

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.

STATA: Help, Search, Tutorials, Manuals, Website

For frequently asked questions see

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

STATA: Help, Search, Tutorials, Manuals, Website

Or we can surf directly from stata

STATA: Help, Search, Tutorials, Manuals, Website

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

STATA: Help, Search, Tutorials, Manuals, Website

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

. help

. search

. tutorial

. findit

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]

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

• Data Entry• Knowing about the Data set

3. Stata & DataStata & Data

Data Entry

Stata Data Sets

Stata data sets have the extension .dta

Reading and saving data files of this extension is simple.

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)

Data Entry

Opening a Stata Data File

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

Data Entry

Opening a Stata Data File

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.

Data Entry

Opening a Stata Data File

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.

Data Entry

Opening a Stata Data File

Data Entry

Opening a Stata Data File

Data Entry

Opening a Stata Data File

The variables of this data file appears in the Variables window

Data Entry

Opening a Stata Data File

Similarly the Review windowkeeps record of the previous commands

Data Entry

Opening a Stata Data File

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

Data Entry

Opening a Stata Data File

Data Entry

Opening a Stata Data File

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

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

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

Data Entry

We type in the Stata Commands as follows. clear

Inputting data in Stata by a type-writer

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

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

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

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

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

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

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

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

Data Entry

Then check its status as:

Inputting data in Stata by a type-writer

Data Entry

Then check its status as:. describe

Inputting data in Stata by a type-writer

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:

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:

Data Entry

Then check its status as:. describe. compress

Inputting data in Stata by a type-writer

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

Data Entry

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

Inputting data in Stata by a type-writer

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

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

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

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

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

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

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

Data Entry

For free format or unformatted data the basic command is:

Stata Command Infile for Reading Non-Stata delimited Data Sets

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

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

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

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

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

Data Entry

. clear

. use nl_test

Stata Data in Stata Editor

Data Entry

. clear

. use nl_test

Stata Data in Stata Editor

Data Entry

. clear

. use nl_test

Stata Data in Stata Editor

Data Entry

. clear

. use nl_test

Stata Data in Stata Editor

Data Entry

. clear

. use nl_test

Stata Data in Stata Editor

Data EntryStata Data in Stata Editor

Save

Data EntryStata Data in Stata Editor

Undo changes since last “save”

Data EntryStata Data in Stata Editor

Sort by selected column

Data EntryStata Data in Stata Editor

Move selected column to the end

Data EntryStata Data in Stata Editor

Hide selected column

Data EntryStata Data in Stata Editor

Delete selected columns or rows

Data EntryStata Data in Stata Editor

Change selected value

Data Entry

. clear

. use nl_test

Exporting Stata Files to other Extensions

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.

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!

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.

Knowing about the Data set1. Describe:

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

Knowing about the Data set1. Describe:

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

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:

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.

Knowing about the Data set2. Code Book:

. clear

. use nl_test

. codebook

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

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).

Knowing about the Data set3. Summarize:

. clear

. use nl_test

Knowing about the Data set3. Summarize:

. clear

. use nl_test

. summarize

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

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.

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.

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

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

Knowing about the Data set4. List:

. clear

. use nl_test

. list v1 in 3/6v1

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

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

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

Knowing about the Data set5. Inspect:

. clear

. use nl_test

Knowing about the Data set5. Inspect:

. clear

. use nl_test

. inspect

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)

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.

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

. describe

. codebook

. summarize

. list

. inspect

Knowing about the Data set

• Variables• Time Variables

4. Stata VariableStata Variable

Like other statistical packages, stata has two sort of variables

• string• numeric

each variables are associated with a label and format.

VariablesVariable Types

For the data nl_test.dta, we type:

. clear

. use nl_test

VariablesRenaming Variables

For the data nl_test.dta, we type:

. clear

. use nl_test

. rename v1 x

VariablesRenaming Variables

For the data nl_test.dta, we type:

. clear

. use nl_test

. rename v1 x

. rename v2 y

VariablesRenaming Variables

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

For the data nl_test.dta, we type:

. clear

. use nl_test

. rename v1 x

. rename v2 y

VariablesLabeling Variables

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

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

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

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

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

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

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

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

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

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.

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

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

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.

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

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

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

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

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

VariablesDoing the entire procedure by Do-Files

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

VariablesDoing the entire procedure by Do-Files

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

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

VariablesDoing the entire procedure by Do-Files

Or we could write . do rawdataread

to run the batch just as the same.

VariablesDoing the entire procedure by Do-Files

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

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

. clear

. input marriage43431342233

VariablesLabelling Variable Values

2213333132end

We then define its value labels as:

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

VariablesLabelling Variable Values

We then define its value labels as:

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

. label list

VariablesLabelling Variable Values

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

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

VariablesRecoding Variable Values

. 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

. 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

. 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

. 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

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

VariablesIndex of the Observations

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

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

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

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

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

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

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

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

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. .

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. .

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. .

. 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. .

. 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. .

. 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. .

. 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. .

. 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. .

. 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 -

. 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. .

. 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. .

. 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

. 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

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.

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

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

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

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

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

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.

VariablesGenerating New Variables

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

. use hs0

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:

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.

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.

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.

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.

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

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

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

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

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

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 (.)

Time VariablesFormat for Time variables

Let us consider the following example:

. clear

. input time

. 15673

. 10345

. 7654

. 3564

. 1023

. 0

. end

Time VariablesFormat for Time variables

Let us consider the following example:

. clear

. input time

. 15673

. 10345

. 7654

. 3564

. 1023

. 0

. end

. format time %d

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

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.

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

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

Time VariablesFormat for Time variables

Let us consider the following example:

. clear

. input time

. 15673

. 10345

. 7654

. 3564

. 1023

. 0

. end

. save datedata

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)

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

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

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

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

• STATA system diagnosis

5. Stata SystemStata System

STATA system diagnosisChecking Status

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

STATA system diagnosisChecking Status

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

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.

STATA system diagnosisDirectory Settings in Windows

We can check the present working directory by the following command

. pwdC:\DATA

STATA system diagnosis

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

. dir

Directory Settings in Windows

STATA system diagnosis

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

. dir

Directory Settings in Windows

STATA system diagnosis

The same thing can be done by typing

. ls

Directory Settings in Windows

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

STATA system diagnosisDirectory Settings in Windows

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

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

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

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.

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:

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:

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:

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:

STATA system diagnosisKeeping Track of Work

To view the log, we do as follows:

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

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

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

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

Summary of Stata system diagnosis:Key Stata Commands:

. describe

. pwd

. dir

. mkdir

. cd

. copy

. log

STATA system diagnosis

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

STATASTATA:: The The WhiteWhite tutorialtutorial