Title: Gereltuya Altankhuyag, LecturerStatistician, UNSIAP
1STATA Third group training course in application
of information and communication technology to
production and dissemination of official
statistics 10 May 11July 2007
Gereltuya Altankhuyag, Lecturer/Statistician,
UNSIAP gereltuya_at_unsiap.or.jp
2Basic commands of graphics
- The commands that draw graphs are
command description --------------
--------------------------------------------------
------ graph twoway scatterplots, line
plots, etc. graph matrix scatterplot
matrices graph bar bar
charts graph dot dot
charts graph box box-and-whisker
plots graph pie pie
charts other more commands to draw
statistical graphs -------------------------------
---------------------------------------
3Basic commands of graphics
- The commands that save a previously drawn graph,
redisplay previously saved graphs, and combine
graphs are - command description
- ------------------------------------------------
---------------------- - graph save save graph to disk
- graph use redisplay graph
stored on disk - graph display redisplay graph
stored in memory - graph combine combine graphs into
one - ------------------------------------------------
----------------------
4Basic commands of graphics
- The commands for printing a graph are
command description ---------------------------
------------------------------------------- graph
print print currently displayed
graph set printcolor set how colors
are printed graph export export .gph
file to PostScript, etc. ------------------------
----------------------------------------------
- All graphs have extension graph
5Basic commands of graphics
- We can generate plots, e.g. a histogram, with
STATA - hist pcexp
- which shows per capita expenditure distnas
skewed
6Basic commands of graphics
- We can induce normality by transforming
consumption on a log scale - gen logpcexp ln(pcexp)
- hist logpce
7Basic commands of graphics
graph bar (mean) pcexp indsave, over(region)
title(Per capita expenditure and individuals'
savings by region)
8Basic commands of graphics
To create a box plot of the household size
vaiable in the hh.dat dataset graph box famsize
9Creation of Do-files and use of Do-file Editor
- Up until now we have used Stata interactively.
- We can do batch processing using Do Files.
- Do files a text file containing commands and
instruct Stata to execute the commands stored in
that file.
10Creation of Do-files and use of Do-file Editor
- We can create Do Files using New Do-file Editor
11Creation of Do-files and use of Do-file Editor
- Do-file Editor has menu bar
- Do-file Editor has tool bar
12Creation of Do-files and use of Do-file Editor
- The do-file editor lets you submit several
commands to Stata at once - General steps of using the do-file
- Click on the Do-file Editor to open do-file editor
13Creation of Do-files and use of Do-file Editor
- Then type the commands that you wish to submit to
Stata
14Creation of Do-files and use of Do-file Editor
- Then click on Do current file
15Creation of Do-files and use of Do-file Editor
- If you wish to execute a subset of the lines in
your do-files, highlight those lines and click
Do current file button.
16Creation of Do-files and use of Do-file Editor
- Tool bar button Run current file is the same as
Do current file, but Run suppressess output. It
is unlikely that you will ever need to use Run
current file button.
17Creation of Do-files and use of Do-file Editor
- You can put comments in your Do-file
- There are ways to include comments in a Do-file
- Begin with asterisk
- Begin with asterisk and forward slash /
- Place the comment in /../ delimiters
- Place the comment after 2 forward slashes //.//
- Place the comment after 3 forward slashes //////
18Creation of Do-files and use of Do-file Editor
19Creation of Do-files and use of Do-file Editor
Advantage of using / / as a comment indicator
The / / comment can be put anywhere at the
end of a line, even at the middle of a line.
Advantage of using as a comment indicator
The comment indicator can be used
interactively.
The / / can be used only in do-files.
20Creation of Do-files and use of Do-file Editor
- By default Stata assumes that each command is
ended by the carriage return (cr) (ENTER key
press). - If a Syntax with commands is too long to fit in
one line, we can handle it in 2 ways - Using delimit
- Using comment signs
21Creation of Do-files and use of Do-file Editor
- using delimit each commandmust end with
semicolon ()
22Creation of Do-files and use of Do-file Editor
- using comment signs ( / /../ //..//
//////)
23Creation of Do-files and use of Do-file Editor
- A do-file stops executing when
- The end of the file is reached
- An exit is executed
- An error occurs
- If an error occurs, the remaining commands in the
do-file are not executed
24Creation of Do-files and use of Do-file Editor
- Retrieve saved do-file
- Click do-file editor
- File
- Open
- Save do-file
- Type in your commands in do-file editor
- File
- Save or Save as
- Saved file takes .do extension
25Creating log-files
- Stata can record your session into a file called
a log file - Log file does not start automatically
- You must tell Stata to record your session
- By default, log files are created in a format
called Stata Markup and Control Language (SMCL)
26Creating log-files
- To create log files using do-files
- Type the followings
- clear
- capture log close
- Specify the location of your log-file in your
do-file - log using C\intropov\logfiles\day2, replace
- Specify the start date and time of running
do-file - di "Job started at S_TIME on S_DATE"
27Creating log-files
- Write all commands and comments accordingly
- Specify the finishing date and time of running
do-file - di "Job ended at S_TIME on S_DATE
- Tell Stata that log is finished
- log close
- Saved file takes .log orsmcl extension
- Note you can make changes in your do-file!!!
-
28Creating log-files
- To run do-file which contains log
- Click on Edit of the do-file menu bar
- Click Select All all commands and comments will
be highlighted - Click Do current file button on tool bar
29Creating log-files
30Creating log-files
31Creating log-files
- To open/retrieve log file click begin log
button on toolbar
32Creating log-files
- Find you log-file name in relevant folder
33Creating log-files
- Log files have two extensions
- filename.smcl Stata produces this file when you
open log - filename.log You can load into your word
processor. Do-file uses this extension!!!
34Creating log-files
- Choose the extension of your log-file by
scrolling down Save as type
35Creating log-files
- Then Stata log options window appears and choose
View existing file (read only) - The click OK button.
36Creating log-files
- Then you will see all your command records and
outputs.
37Creating log-files
38Creating log-files
- Append to an existing log-file
- log using filename, append
- Replace an existing log-file
- log using filename, replace
39Use of ado-files
- Stata has two types of commands in terms of use
- Commands built into Stata
- Ado-file commands
- An ado-file is an ASCII text file that contains a
Stata program. - Ado-files usually come with help files.
40Use of ado-files
- To determine your command
- which command name
- which ci
- . which ci
- \\Unitednations\Stata9\ado\base\c\ci.ado
- ! version 3.3.15 17mar2005
- Which summarize
- . which summarize
- built-in command summarize
41Use of ado-files
- You can view ado-files
- . viewsource ci.ado
42Use of ado-files
- To know where are ado-files on your PC
- sysdir
sysdir STATA \\Unitednations\Stata9\ UPDATES
\\Unitednations\Stata9\ado\updates\ BASE
\\Unitednations\Stata9\ado\base\ SITE
\\Unitednations\Stata9\ado\site\ PLUS
c\ado\plus\ PERSONAL
c\ado\personal\ OLDPLACE c\ado\
43Use of ado-files
- To run ado-files
- ado-filename varname
- Examples
- ci pcexp
- gini pcexp
- lorenz pcexp
44Use of ado-files
- Examples
- . ci pcexp
- Variable Obs Mean Std. Err.
95 Conf. Interval - -------------------------------------------------
--------------------------- - pcexp 519 6901.953 192.7895
6523.207 7280.698 - . gini pcexp
- Gini coefficient .26204428
-
45Use of ado-files
. lorenz pcexp
46Inputting data
- To input data into Stata, you can use
47Inputting data
48Inputting data
49Inputting data
- Suppose we have an ASCII text dataset as shown
below -
50Inputting data
- Suppose we have a dataset countries.txt in
c\intropov\data\ folder - Use the infile command with syntax
- infile variable-list using filename.raw
- In particular, enter
- cd c\intropov\data
- infile y_a y_b y_c using countries.txt
51Inputting data
- When using the infile command for character
(string) variables (eg.hhcode), we need to
identify these variables. For instance - infile y_a y_b y_c str30 domain using
countries.txt - For more details regarding infile, enter
- help infile1
-
52Inputting data
- Suppose we have a dataset try2.txt in
c\intropov\data folder with the data in
specific fields -
Assumes last line is blank line
53Inputting data
- Use the infix command to input into Stata
- infix domain 1 hhcode 2 age 3-4 using
try2.txt, clear - Infix accepts an if statement, e.g.,
- infix if domain 1
-
54Inputting data
- IMPORTING A WORKSHEET
- Suppose we have an Excel worksheet called
countries2.xls also in the c\intropov\data
folder. - Read the file with Excel, then SAVE AS a comma
delimited CSV file. - Enter
- insheet using countries2.csv, clear
- Note Stata cannot read/import data directly from
Excel!!
55Inputting data
- Thus, Stata can read text files with
- Infile (if the data in text is separated by
spaces and does not have strings, or if strings
are just one word, or if all strings are enclosed
in quotes) - Infix (fixed format text)
- Insheet (if text file was created by a
spreadsheet or db program)
56Exporting data
- The command outfile save the data in ASCII
outsheet in a comma delimited format. - File ? Export ?Comma or tab- separated data
57Inputting and exporting datasets
- There are third party programs, esp.
Stat/Transfer and DBMS/COPY, that perform
translations from one data format (e.g., Excel,
SAS, SPSS, Stata) to another.
58Inputting and exporting datasets
59Inputting data
- There are 2 ways of creating a new dataset in
Stata - 1. Use of Edit
- 1.1 Open the STATA spreadsheet editor
60Inputting data
- 1.2 Enter data into the editor, when done close
the editor. - 1.3 Save dataset
61Inputting data
- 2. Use of input command
- 2.1 input hhcode pid age
- 2.2 enter values of variables
- 2.3 end
- 2.4 Save dataset
-
62Inputting data
- NOTE
- We can copy and paste values from a spreadsheet
- Data can be entered variable-by- variable or
observation-by-observation - When entering data variable-by-variable, use TAB
for observation-by-observation use ENTER key - Missing data code is a period .
63Inputting data
- NOTE
- Numerical values SHOULD NOT include commas else
these are considered string. - String variable values can be almost any
combination of letters, numbers, symbols up to 80
characters long in Intercooled/ Small and up to
244 characters in STATA/SE.
64Summary of STATA session
65Summary
- Stata Files
- .log records commands and outputs
- .do contains a sequence of Stata
commands for batch execution - .dta data files
- .graph graphics files
- .ado for writing new commands
-
66Summary
- Basic Commands
- Interrupt Stata Ctrl Break or Stop button
- Exit from Stata type exit or from File menu,
choose exit or clear - Help
- Search
67Summary
- To inspect datasets
- codebook display contents of variables
- count counts cases
- describe display properties of variables
- summarize descriptive stats
- list show values
- tabulate one or two way freq tables
- table higher dim freq tables
- tabstat tables with statistics
- Note weigh is important!!!!
68Summary
- To create and change varaibles
- generate/egen create new variable
- replace replace old values
- recode change values by conditions
- label label var (up to 80 chars)
69Summary
- To reorganize datasets
- rename change the name of a variable
- drop eliminate variables or observations
- keep keep variables or observations
- clone copy of an existing variable
- sort arrange into ascending order
- gsort arrange in ascending or descending order
70Summary
- To reorganize datasets
- append append two datasets
- merge merges two datasets
- reshape converts dataset from wide to long and vs
71Summary
- Survey Data Analysis
- svy commands
- svyset
- svymean
- svytotal
- svytab
- syyprop
- svyreg
72Summary
- Importing Datasets
- Files must be in text delimited form (.txt)
- Use infile function to import
- infile var1 var2 etc using filename.txt, clear
- Or the infix or insheet functions
- Or use StataTransfer software
-
73 Please perform EXERCISE 4