Gereltuya Altankhuyag, LecturerStatistician, UNSIAP - PowerPoint PPT Presentation

1 / 73
About This Presentation
Title:

Gereltuya Altankhuyag, LecturerStatistician, UNSIAP

Description:

Use 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 ... – PowerPoint PPT presentation

Number of Views:93
Avg rating:3.0/5.0
Slides: 74
Provided by: un6
Category:

less

Transcript and Presenter's Notes

Title: Gereltuya Altankhuyag, LecturerStatistician, UNSIAP


1
STATA 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
2
Basic 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 -------------------------------
---------------------------------------
3
Basic 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
  • ------------------------------------------------
    ----------------------

4
Basic 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

5
Basic commands of graphics
  • We can generate plots, e.g. a histogram, with
    STATA
  • hist pcexp
  • which shows per capita expenditure distnas
    skewed

6
Basic commands of graphics
  • We can induce normality by transforming
    consumption on a log scale
  • gen logpcexp ln(pcexp)
  • hist logpce

7
Basic commands of graphics
graph bar (mean) pcexp indsave, over(region)
title(Per capita expenditure and individuals'
savings by region)
8
Basic commands of graphics
To create a box plot of the household size
vaiable in the hh.dat dataset graph box famsize
9
Creation 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.

10
Creation of Do-files and use of Do-file Editor
  • We can create Do Files using New Do-file Editor

11
Creation of Do-files and use of Do-file Editor
  • Do-file Editor has menu bar
  • Do-file Editor has tool bar

12
Creation 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

13
Creation of Do-files and use of Do-file Editor
  • Then type the commands that you wish to submit to
    Stata

14
Creation of Do-files and use of Do-file Editor
  • Then click on Do current file

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

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

17
Creation 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 //////

18
Creation of Do-files and use of Do-file Editor
19
Creation 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.
20
Creation 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

21
Creation of Do-files and use of Do-file Editor
  • using delimit each commandmust end with
    semicolon ()

22
Creation of Do-files and use of Do-file Editor
  • using comment signs ( / /../ //..//
    //////)

23
Creation 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

24
Creation 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

25
Creating 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)

26
Creating 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"

27
Creating 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!!!

28
Creating 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

29
Creating log-files
30
Creating log-files
31
Creating log-files
  • To open/retrieve log file click begin log
    button on toolbar

32
Creating log-files
  • Find you log-file name in relevant folder

33
Creating 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!!!

34
Creating log-files
  • Choose the extension of your log-file by
    scrolling down Save as type

35
Creating log-files
  • Then Stata log options window appears and choose
    View existing file (read only)
  • The click OK button.

36
Creating log-files
  • Then you will see all your command records and
    outputs.

37
Creating log-files
38
Creating log-files
  • Append to an existing log-file
  • log using filename, append
  • Replace an existing log-file
  • log using filename, replace

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

40
Use 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

41
Use of ado-files
  • You can view ado-files
  • . viewsource ci.ado

42
Use 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\
43
Use of ado-files
  • To run ado-files
  • ado-filename varname
  • Examples
  • ci pcexp
  • gini pcexp
  • lorenz pcexp

44
Use 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

45
Use of ado-files
. lorenz pcexp
46
Inputting data
  • To input data into Stata, you can use

47
Inputting data
48
Inputting data
49
Inputting data
  • Suppose we have an ASCII text dataset as shown
    below

50
Inputting 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

51
Inputting 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

52
Inputting 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
53
Inputting 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

54
Inputting 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!!

55
Inputting 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)

56
Exporting data
  • The command outfile save the data in ASCII
    outsheet in a comma delimited format.
  • File ? Export ?Comma or tab- separated data

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

58
Inputting and exporting datasets
59
Inputting data
  • There are 2 ways of creating a new dataset in
    Stata
  • 1. Use of Edit
  • 1.1 Open the STATA spreadsheet editor

60
Inputting data
  • 1.2 Enter data into the editor, when done close
    the editor.
  • 1.3 Save dataset

61
Inputting 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

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

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

64
Summary of STATA session
65
Summary
  • 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

66
Summary
  • Basic Commands
  • Interrupt Stata Ctrl Break or Stop button
  • Exit from Stata type exit or from File menu,
    choose exit or clear
  • Help
  • Search

67
Summary
  • 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!!!!

68
Summary
  • 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)

69
Summary
  • 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

70
Summary
  • To reorganize datasets
  • append append two datasets
  • merge merges two datasets
  • reshape converts dataset from wide to long and vs

71
Summary
  • Survey Data Analysis
  • svy commands
  • svyset
  • svymean
  • svytotal
  • svytab
  • syyprop
  • svyreg

72
Summary
  • 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
Write a Comment
User Comments (0)
About PowerShow.com