Introduction to Stata 7.0

October 30, 2017 | Author: Anonymous | Category: N/A
Share Embed


Short Description

to SPSS. Reference manual (the 1-volume extract or the full 4-volume manual) .. The second. Svend Juul ......

Description

Introduction to Stata 7.0 By Svend Juul

Department of Epidemiology and Social Medicine, University of Aarhus. September 2002.

Contents 1. 2. 3. 4.

Installing, customizing and updating Stata . Windows in Stata . . . . . . . . . . . . . . . . . . . . Getting help . . . . . . . . . . . . . . . . . . . . . . . . Executing Stata commands . . . . . . . . . . . . .

3 5 7 8

4.1. Issuing single commands . . . . . . . . . . . 8 4.2. Do-files . . . . . . . . . . . . . . . . . . . . . . 8 5. Stata file types and names . . . . . . . . . . . . . . 9 6. Variables and observations . . . . . . . . . . . . 10 6.1. Variable names . . . . . . . . . . . . . . . . 10 6.2. Numeric variables . . . . . . . . . . . . . . 11 6.3. Missing values . . . . . . . . . . . . . . . . 11 7. Command syntax . . . . . . . . . . . . . . . . . . . 12 8. Getting data into Stata . . . . . . . . . . . . . . . . 16 9. Documentation commands . . . . . . . . . . . . 18 10. Modifying data . . . . . . . . . . . . . . . . . . . . . 20 10.1. Calculations . . . . . . . . . . . . . . . . . . 20 10.2. Selections . . . . . . . . . . . . . . . . . . . 22 10.3. Renaming and reordering variables . . . 23 10.4. Sorting data . . . . . . . . . . . . . . . . . . 23 10.5. Numbering observations . . . . . . . . . . 24 10.6. Aggregating (collapsing) data . . . . . . . 24 11. Description and analysis . . . . . . . . . . . . . . 25 11.1. Categorical data . . . . . . . . . . . . . . . 26 11.2. Continuous data . . . . . . . . . . . . . . . 29 11.3. Graphs . . . . . . . . . . . . . . . . . . . . . 30 12. Regression models . . . . . . . . . . . . . . . . . . 31 12.1. Linear regression . . . . . . . . . . . . . . 31 12.2. Logistic regression . . . . . . . . . . . . . 32 13. Survival and related analyses . . . . . . . . . . 33 14. Combining files . . . . . . . . . . . . . . . . . . . . 37 15. Miscellaneous . . . . . . . . . . . . . . . . . . . . . . 38 15.1. Memory considerations . . . . . . . . . . . 38 15.2. String variables . . . . . . . . . . . . . . . . 39 15.3. Dates. Danish CPR numbers . . . . . . . 41 15.4. Random samples, simulations . . . . . . . 43 15.5. Immediate commands . . . . . . . . . . . . 44 15.6. Sample size and power estimation . . . . 45 15.7. Ado-files . . . . . . . . . . . . . . . . . . . . 46 15.8. Exchange of data with other programs . 48 15.9. For old SPSS users . . . . . . . . . . . . . 48 16. Do-file examples . . . . . . . . . . . . . . . . . . . . 50

Appendix 1: Purchasing Stata and manuals . . . 52 Appendix 2: Entering data with EpiData . . . . . 53 Appendix 3: NoteTab Light: a text editor . . . . 55 Alphabetic index . . . . . . . . . . . . . . . . . . . . . . . . 56

Preface This booklet is a short introduction to Stata version 7. Stata is a software package designed for data management and statistical analysis; the primary target group is academia. The guide does not replace the manuals. It is intended mainly for the beginner, but knowledge of fundamental Windows functions is necessary. It is intended for self-instruction, using exercises1. Only basic commands and a few more advanced analyses are shown. You will find a few examples of output, but during exercises you will get more experience about what kinds of output Stata can create. You communicate with Stata by entering commands rather than by pointing with the mouse. This may seem inconvenient, but at the same time you get a straightforward documentation of what you did. This mode of operation is a contrast to working with spreadsheets where you can do many things almost by intuition, but where the documentation of what you did – including the sequence of actions – may be extremely difficult to reconstruct. Managing and analysing data is more than issuing commands, one at a time. In my booklet Take good care of your data2 I give advice on documentation and safe data management, with Stata examples. The main message is: Keep the audit trail. Primarily for your own sake, secondarily to enable external audit or monitoring. These considerations are also reflected in this booklet. To users with SPSS experience: By design Stata is lean compared to SPSS. Concerning statistical capabilities Stata can do a lot more. Stata has a vivid exchange of ideas and experiences with the academic users while SPSS increasingly targets the business world. Just compare the home pages www.spss.com and www.stata.com. Or try to ask a question or give a suggestion to each of the companies. In section 15.9 I show the basic SPSS commands and their Stata counterparts. On the purchase of program and manuals, see Appendix 1. I welcome any comments, complaints and suggestions. My e-mail address is: [email protected].

Aarhus, September 2002 Svend Juul

1)

See www.svf-it.au.dk for exercises (not yet in place).

2)

Juul S. Take good care of your data. Aarhus, Department of Epidemiology and Social Medicine, 2001. (Free copy: mail to [email protected] – or download from www.svf-it.au.dk – not yet in place).

1

Notation etc. in this booklet Stata commands are shown like this: tabulate agegr sex , chi2 tabulate and chi2 are Stata words, shown with italics, agegr and sex is variable

information, shown with ordinary typeface. In some of the output examples you will see the commands preceded by a starting period: . tabulate agegr sex , chi2

This is how commands look in output, but you should not type the period yourself when entering a command. Optional parts of commands are shown with a light typeface and enclosed in light typeface square brackets. Note that square brackets may also be part of a Stata command, in that case they are shown with the usual bold typeface. Comments are shown with light typeface: save c:\dokumenter\proj1\alfa1.dta [ , replace] summarize bmi [fweight=pop] /* Weights in square brackets */

In the examples I use c:\tmp as my folder for temporary files, while Windows' standard temporary folder is c:\windows\temp. You may, of course, use whatever you want. In the Stata manuals it is assumed that you use c:\data for all of your Stata files. I strongly discourage that suggestion. I am convinced that files should be stored in folders reflecting the subject, not the program used; otherwise you could easily end up confused. You will therefore see that I always give a full path in the examples when opening (use) or saving files (save). Throughout the text I use Stata's style to refer to the manuals: [GSW] Getting Started with Stata for Windows [U] User's Guide [R] Reference manual (the 1-volume extract or the full 4-volume manual) [G] Graphics manual [P] Programming manual See more on manuals in section 3 and appendix 1

2

1. Installing, customizing and updating Stata 1.1. Installing Stata

[GSW] 1

No big deal, just insert the CD and follow the instructions. By default Stata will be installed in the c:\Stata folder. 'Official' .ado files will be put in c:\Stata\ado, 'unofficial' in c:\ado. To get information about the locations, enter the Stata command sysdir. Soon after you installed Stata enter in Stata's command line window (see section 2): update query

to get the most recent modifications and corrections of bugs from the net; see section 1.3.

Also install StataQuest StataQuest gives you to get access to extended menus, enabling you to create a number of commands by the menu system. To install StataQuest issue the following commands from Stata's command window (you must be on the internet): net net net net

cd http://www.stata.com/quest7 install quest1 install quest2 install quest3

Issue the command quest on to get access to the extended menus. If you want to use StataQuest regularly (I do), I recommend to include the command in profile.do, as shown in section 1.2.

Also install NoteTab Light As described in section 2, Stata has some shortcomings in handling output, and you will benefit a lot from a standard text editor. I recommend NoteTab Light; see appendix 3.

1.2. Customizing Stata Create desktop shortcut icon In the Start Menu you find the Stata shortcut icon. Right-click it and drag it to the desktop to make a copy. Next right-click the desktop shortcut icon. In the Path field you see e.g. c:\stata\wstata.exe /m1, meaning that 1 MB of memory is reserved for Stata. You might need to change this, see section 15.1 on memory considerations. As the start folder you probably see c:\data. I suggest to change the start folder to c:\dokumenter or whatever your personal main folder is. The reasons for this suggestion: • You should put your own text-, data- and do-files in folders organised and named by subject, not by program, otherwise you will end up confused. • All of your 'own' folders should be sub-folders under one personal main folder, e.g. c:\dokumenter. This has at least two advantages:

3

- You avoid mixing your 'own' files with program folders - You can set up a consistent backup strategy. For further advice, see Take good care of your data.

The profile.do file

[GSW] 17, A7

If you put a profile.do file in the start folder defined above (c:\dokumenter) the commands will be executed automatically every time you open Stata. A more elaborate profile.do is shown in section 16 (it writes date and time to the log files). Write your profile.do with Stata's Do-file editor or with NoteTab (see appendix 3). * c:\dokumenter\profile.do quest on set logtype text log using c:\tmp\stata.log , replace cmdlog using c:\tmp\cmdlog.txt , append

/* /* /* /*

activate StataQuest */ simple text output log */ open output log */ open command log */

The meaning of the commands I recommend are: quest on activates StataQuest to show extended menus. set logtype text writes simple ASCII text (not SMCL) in the output log, to enable displaying it in e.g. NoteTab. log opens Stata's log file (stata.log) to receive the full output; the replace option overwrites old output. The folder (c:\tmp) must exist beforehand. cmdlog opens Stata's command log file (cmdlog.txt); the append option keeps the command log from previous sessions and lets you examine and re-use past commands.

Fonts. Window sizes and locations

[GSW] 18

In each window (see section 2) you may click the upper left window button; one option is to select font for that type of window. Select a fixed width font, e.g. Courier New 10 pt or Letter Gothic Bold 9 pt . You may adjust window sizes and locations with the mouse. When finished, make your choices the default by: Preferences < Save windowing preferences

1.3. Updating Stata

[GSW] 20; [U] 32

Stata's system for updating the program is excellent. Updates and new facilities are made available at the internet, and the user interface is very friendly. If you want to examine whether any modifications have been published, enter the command: update query

You will now be told whether your versions of the main program (the executable) and the .ado files are the most recent or whether you should update. Just follow the instructions.

4

2. Windows in Stata

[GSW] 2

Typical placement of four of the Stata windows.  Review

 Stata Results

use c:\dokumenter\proj summarize generate bmi=weight/(h regress bmi age sex

. generate bmi=weight/(height^2) . regress bmi age sex Source | SS df MS -------------+-----------------------------Model | 982.996648 2 491.498324 Residual | 94516.5394 227 416.37242 -------------+-----------------------------Total | 95499.536 229 417.028542

 Variables id sex age height weight bmi

Number of obs F( 2, 227) Prob > F R-squared Adj R-squared Root MSE

= = = = = =

230 1.18 0.3090 0.0103 0.0016 20.405

-----------------------------------------------------------------------------bmi | Coef. Std. Err. t P>|t| [95% Conf. Interval] -------------+---------------------------------------------------------------age | .1452639 .0947552 1.53 0.127 -.0414483 .3319761 sex | .0681109 3.074339 0.02 0.982 -5.989781 6.126003 _cons | 15.89703 6.938118 2.29 0.023 2.225677 29.56838 ------------------------------------------------------------------------------

 Stata Command regress bmi age sex

Command line window In this one line window you may enter single commands and execute them by [Enter]. For major work you should not use the Command line window, but create do-files (see section 4.2) with Stata's do-file editor or a text editor, eg. NoteTab.

Review window

[GSW] 10

This window displays the most recent commands. If you click a command in the Review window it is pasted to the Command line window where you may edit and execute it. Save the commands in the Review window as a do-file by clicking the upper left Review window button and select Save Review Contents.

Variables window

[GSW] 10

You see a list of the variables in memory. Paste a variable name to the Command line window by clicking it.

Results window This is the primary display of the output. It has, surprisingly, severe limitations: • its size is limited, and you can only access the last few pages of output. • you can't edit it, e.g. by adding comments or removing junk. • You may print the contents, but the log file is better suited for that.

5

Viewer window

[GSW] 3

 Stata Viewer [Advice on Help] Back Refresh Search Command: help viewer

Help!

Contents

What's New

News

Using the Viewer

(manual: [GS])

In the Viewer, you can see help for contents or help for any Stata command search help files, documentation, and FAQs (advice on using search) find and install STB and user-written programs from the net review, manage, and uninstall user-written programs check for and optionally install official updates view your logs or any file launch your browser see the latest news from www.stata.com

The main use of this window is viewing help files (see help and search, section 3). You may select part of the viewer window for printing, using the mouse – but unfortunately not the keyboard – to highlight it. Stata also suggests that you use it for viewing and printing output (the log file), but it does not work well, and I find it much handier to use a general text editor (e.g. NoteTab Light, see section 1.4) for examining, editing and printing output.

Data window

[GSW] 9

The Data window looks like a spreadsheet, and you may use it for entering small amounts of data, but I don't recommend that, see section 8. A data entry program proper should be preferred, e.g. EpiData, see appendix 2. In [GSW] 9 it is demonstrated how to use the edit command to make corrections directly in the data window. For reasons of safety and documentation I strongly discourage that. Modifications to data should be made with do-files, see Take good care of your data. The browse command enables you to see but not modify data in the Data window. To see a specific cell issue the command: browse age in 23

/* the variable age in the 23rd observation */

Do-file editor

[GSW] 15

This is a standard text editor used for writing do-files (see section 4.2). The do-file editor has the special feature that you may request Stata to execute all or a selection of the commands by clicking the Do-button. I prefer NoteTab Light to the do-file editor; see appendix 3.

6

3. Getting help

[GSW] 4, 19, 20; [U] 2, 8

3.1. The manuals To most users the following manuals (see appendix 1) will suffice: • Getting Started manual [GSW] • User's Guide [U] • Reference Manual Extract [R] (one volume) In this booklet, in the manuals, and in the online help [GSW], [U] and [R] refer to sections in the manuals. Getting Started [GSW] illustrates the main features. I recommend to read this in full; it is very friendly. Parts of User's Guide are useful too while the Reference Manual is used for looking up details about commands.

3.2. Online help

[GSW] 4; [U] 8, 32

Online help is best displayed in the Viewer window (see section 2). Issue help and search from the Viewer's command line, whelp and findit from Stata's command line.

help and whelp If you know the command name (e.g. tabulate) see the help file (tabulate.hlp) by: from the Viewer command line: help tabulate or from Stata’s command line window: whelp tabulate The help file is displayed in the Viewer window, and from here you may print it. You may also use the links included. Try it.

search and findit You need not know the command name. Get information about nonparametric tests by: from the Viewer command line: search nonparametric In the Viewer window you now get overview of relevant commands with links to help files. To see all information on your computer and on the net on goodness-of-fit tests with poisson regression use findit: from Stata’s command line window: findit poisson goodness

Error messages Stata's short error messages include a code, e.g. r(131). To get more clues enter: search r(131)

Some error messages demonstrate that Stata's artificial intelligence isn't fully developed yet: . ttest bmichange by(drug) time-series operators not allowed

The error actually has nothing to do with time series analysis, but by(drug) is an option to ttest and should be preceded by a comma (see section 7): ttest bmichange , by(drug)

7

4. Executing Stata commands 4.1. Issuing single commands In the Command line window you may enter commands, one at a time, and execute them. A copy of the command is written in the Review window. This works well in simple tasks, but with complex tasks it is a lot safer and more efficient to write the commands to a do-file before execution. You may, after having issued a number of more or less successful commands, save the contents of the Review window (click the upper left Review window button) as a do-file which probably needs some editing (use Stata's do-file editor or NoteTab) before saving the final version.

StataQuest If you installed StataQuest (see section 1.1) a number of commands can be created by clicking the menus. This is useful for some more complex tasks – but you will soon learn that it is much more efficient to learn the basic commands and enter them – preferably in a do-file.

4.2. Do-files

[U] 19

A do-file is a series of commands to be executed in sequence. Use the do-file editor or NoteTab to enter the commands. For any major tasks this is preferable to entering single commands because: • The do-file serves as documentation of what you did. • If you discover an error you can easily correct it and re-run the do-file. • You make sure that the commands are executed in the sequence intended. In section 16 I show examples of do-files and give important recommendations on the naming of do-files.

The do command Execute the do-file c:\dokumenter\proj1\alpha.do from the command line by: do c:\dokumenter\proj1\alpha.do

I don't recommend the run command because the single commands remain invisible to you, and you thus miss important documentation. From the do-file editor you may execute all of the current file by clicking the do-button (number two from the right), or you may execute a highlighted part of the file. The disadvantage of this method is that the name of your do-file is not reflected in the output. I recommend to issue a do command with full path and name of the do-file, for reasons of documentation.

8

5. Stata file types and names

[U] 14.6

.dta files: Stata data The extension for a Stata data set is .dta. Stata data sets can only be created and interpreted by Stata itself (and by translation programs, see section 15.8).

.do files: command files A .do file is a number of commands to be executed in sequence. You may issue single commands in the command line window, but if you are doing anything substantial you should do it with a .do file. You find examples in section 16 and some useful examples in Take good care of your data. In both places I emphasize the importance of a system for naming do-files.

.ado files: programs An .ado file is a program. For more information see section 15.7.

.hlp files: Stata help Stata's online documentation is kept in .hlp files, written in SMCL format (resembling XML). More information in section 3.

.gph files: graphs

[GSW] 16; [G] (Graphics manual)

Stata graphs can be saved as .gph files.

.dct files: dictionary files

[U] 24; [R] infile

Fixed format ASCII data may be read with infile using a dictionary file. I did not demonstrate this option in section 8.

9

6. Variables and observations A Stata data set is rectangular; here is one with four observations and five variables: Variables age

sex

height

weight

1

27

1

178

74

2

54

2

166

67

3

63

1

173

85

4

36

1

182

81

Observations

obsno

The variable age in the third observation can be referred to as age[3]. The principle is: First observation: age[1] Last observation age[_N] Current observation: age or age[_n] Previous (lag) observation: age[_n-1] Next (lead) observation: age[_n+1] Observation 27 age[27] In section 10.5 I show how to use _n and _N to assign numbers to observations.

6.1. Variable names Variable names can be 1-32 characters, but Stata often abbreviates long variable names, and if you translate to other programs you will get trouble, so I recommend to use only 8 characters. The letters a-z (but not æøå), the numbers 0-9 and _ (underscore) are valid characters. Names must start with a letter (or an underscore, but this is discouraged because many Stata-generated variables start with an underscore). These are valid variable names: a

q17

q_17

pregnant

sex

Stata is case-sensitive Variable names may include lowercase and uppercase letters, but Stata is case-sensitive: sex and Sex are two different variable names. Throughout this booklet I use lowercase variable names; anything else would be confusing.

6.2. Numeric variables

[U] 15.2

Most often you can do without worrying about numeric types, but for understanding missing values you should know this (also see section 15.1 on Memory considerations):

10

Type Integer

byte int long

Floating point

Range

Imprecision

Bytes

Missing value

±126 ±32,766 ±2,147,483,646

0 0 0

1 2 4

127 32,767 2,147,483,647

±1036 ±10308

6×10–8 2×10–16

4 8

2128 21023

float double

Numeric formats

[U] 15.5.1

If no format is specified Stata uses General format, presenting values as readable and precise as possible. So in most cases you need not bother with numeric formats. Format

Formula

Example

%2

1000

10,000,000

General

%w.dg

%9.0g

1.414214

1000

1.00e+07

Fixed

%w.df

%9.0f

1

1000

10000000

%9.2f

1.41

1000.00

10000000.00

%10.3e

1.414e+00

1.000e+03

1.000e+07

Exponential

%w.de

w: The total width including period and decimals Example:

d: The number of decimals

format dollars kroner %6.2f

6.3. Missing values

[U] 15.2.1

Stata's missing values, shown by . (period), are omitted from calculations. They are created: – in input when a numeric field is empty – by calculations that are not valid, e.g. division by 0 – by calculations when one of the variables is missing. Stata defines the highest possible value for each data type as missing; for a byte variable this value is 127. This may confuse you (it confused me) in conditions: list age if age > 65

lists all whose age is > 65, and those with missing age. To exclude the missing: list age if age > 65 & age ~=.

Unlike SPSS you can not declare certain codes as missing. I recommend to use standard 'impossible' values for missing, eg.: –1 Question not asked (N of pregnancies for a male respondent) –2 Question asked, no response –3 Response: Don't know To avoid losing information, do not recode these codes to Stata's missing in the database; wait till just before analysis. mvdecode is useful; this recodes values –1 to –3 to . (missing): mvdecode _all , mv(-1/-3) [R] mvencode 11

7. Command syntax

[U] 14.1

All Stata commands must be written in lowercase. Variable names may include lowercase and uppercase letters, but Stata is case-sensitive: sex and Sex are two different variable names. Throughout this booklet I use lowercase variable names; anything else would be confusing. Uppercase variable names are sometimes used within programs (ado-files, see section 15.7) to avoid interference with the variable names in the data set. The general syntax of Stata commands can be written like this: [prefix:] command [varlist] [qualifiers] [, options] or more detailed like this: [prefix:] command [varlist][if expression][in range][weight][, options]

Qualifiers and options Qualifiers are general to many commands. See below on if, in and weights. Options are specific to a command. The option list is preceded by a comma. Violating this rule is probably the most frequent cause of error messages.

Command examples Here are examples with the command summarize (mean, minimum, maximum etc.): summarize statistics for all variables in data set; the same as: sum _all statistics for all variables in data set (note abbreviation) sum sex age statistics for sex and age sum sex-weight statistics for the consecutive variables sex to weight sum age if sex==1 statistics for males only sum bmi in 1/10 statistics for observation 1-10 bysort sex: summarize separate statistics for each sex. sum pro* , detail detailed information on all variables starting with pro sum bmi [fweight=pop] observations are weighted by the value of pop

Variable lists

[U] 14.1.1

A variable list (varlist) calls one or more variables to be processed. Examples: (nothing) often the same as _all _all all variables in the data set sex age pregnant three variables pregnant sex-weight pregnant and the consecutive variables sex to weight pro* all variables starting with pro

12

In commands that have a dependent variable, this is the first in the varlist: oneway bmi sex bmi is the dependent variable regression bmi sex age bmi is the dependent variable graph weight height scatterplot, weight is y-axis tabulate expos case in tabulate the first variable defines the rows

Conditional commands. The if qualifier

[U] 14.1.3.

The operators used in conditions are defined in section 10.1. Here are a few examples: summarize age if sex==1 statistics for males only list id age if age < 35 list only if age < 35 replace npreg=. if sex==1 set npreg to missing for males

Numeric ranges. The in qualifier

[U] 14.1.4

Numeric ranges are marked by a slash: list sex age weight in 1/10 recode agegrp 1/9.999=1 10/19.999=2

observations 1 to 10

Weighting observations

[U] 14.1.6, [U] 23.13

A typical use is to 'multiply' observations when the input is tabular: Cases

Controls

Exposed Unexposed

21 23

30 100

Total

44

130

. input expos case pop 1 1 21 1 0 30 0 1 23 0 0 100 . end . tabulate expos case [fweight=pop] , chi2 . cc expos case [fweight=pop]

/* see section 8 */

/* see section 11.1 */ /* see section 11.1 */

Immediate commands (see section 15.5) use tabular input directly.

13

by and bysort prefix

[U] 14.5

Makes a command display results for subgroups of the data. Data must be pre-sorted by the byvariable: sort sex by sex: summarize age height weight

or, in one line: bysort sex: summarize age height weight

Text strings, quotes Stata requires double quotes, not single quotes, around text strings, but you may omit quotes unless the string has embedded blanks or commas: label define sex

1 male

2 female

9 "sex unknown"

You need not use quotes around filenames: save c:\dokumenter\proj1\alfa1.dta

unless they include blank space: save "c:\dokumenter\project 1\alfa1.dta"

Comments

[U] 19.1.2

Lines that begin with * are interpreted as comments, enabling you to include short explanations in a do-file. Also, a text surrounded by /* and */ is interpreted as a comment – even if it is written within a command or wraps over several lines. The purpose of comments is only to make do-files more readable to yourself – but that may be extremely useful. * C:\DOKUMENTER\PROFILE.DO executes when opening Stata summarize bmi , detail

/* Body mass index */

Long command lines

[U] 19.1.3

By default a command ends when the line ends (carriage return), and no special delimiter terminates commands. However, command lines in do-files should be no longer than 80 characters. The continuation line problem is solved by 'commenting out' the carriage return. recode opagr 0/14.999=1 14.999/34.999=2 34.999/54.999=3 /* */ 54.999/120=4

Another option is to define ; (semicolon) as the future command delimiter: #delimit ; /* Semicolon delimits future commands */ recode opagr 0/14.999=1 14.999/34.999=2 34.999/54.999=3 54.999/120=4 ; tab1 opagr ; #delimit cr /* Back to normal: Carriage return delimiter */

14

Number lists

[U] 14.1.8

A number list (numlist) is a list of numbers; there are some shorthand possibilities: 1(3)10 means 1 4 7 10 1(1)4 4.5(0.5)6 means 1 2 3 4 4.5 5 5.5 6 4 3 2 7(-1)1 means 4 3 2 7 6 5 4 3 2 1 (Danish CPR number test) 1/5 means 1 2 3 4 5 4/2 7/1 means 4 3 2 7 6 5 4 3 2 1 (Danish CPR number test)

for

[R] for

Enables you with few command lines to repeat a command. To do the modulus 11 test for Danish CPR numbers (see section 15.3) first multiply the digits by 4,3,2,7,6,5,4,3,2,1; next sum these products; finally check whether the sum can be divided by 11. The CPR numbers were split into 10 one-digit numbers c1-c10: generate test=0 for C in varlist c1-c10 \ X in numlist 4/2 7/1 : replace test=test+C*X replace test=mod(test,11) list id cpr test if test ~=0 C and X are stand-in variables (names to be chosen by yourself; note the use of capital letters

to avoid interference with existing variables), to be sequentially substituted by the elements in the corresponding list. Each list must be declared by type; there are four types: newlist list of new variables varlist list of existing variables numlist list of numbers anylist list of words In the for command the first list is 10 existing variables, hence the varlist. The second list is 10 numbers, hence the numlist. The 10 elements in the first list sequentially replace C, and the 10 elements in the second list sequentially replace X. This unfolds to 10 commands like: replace test=test + c1*4 replace test=test + c2*3

etc.

In section 11.1 (tab2) I present another example of the use of for.

15

8. Getting data into Stata

[U] 24; [GSW] 7

On exchange of data with other programs, see section 15.8.

Open Stata data

[R] save

Read an existing Stata data set from disk into memory by: use c:\dokumenter\p1\a.dta [ , clear]

If there are data in memory, use will be rejected unless you specify the clear option. If you want only observations that meet a condition: use c:\dokumenter\p1\a.dta if sex==1

If you want the first 100 cases only: use c:\dokumenter\p1\a.dta in 1/100

If you want to work with only a subset of variables: use age sex q1-q17 using c:\dokumenter\p1\a.dta

Save Stata data

[R] save

Save the data in memory to a disk file by: save c:\dokumenter\p1\a.dta [ , replace]

If you already have a disk file with this name, your request will be rejected unless you specify the replace option. Only use the replace option if you really want to overwrite data.

Entering data with EpiData To enter data I recommend EpiData, available for free from www.epidata.dk. This easy-to-use program has all the facilities needed. Further information in appendix 2.

Entering data as commands or in a do-file

[R] input

Very small data sets. Define the variables with the input command and enter the values. Finish with end. It can be done interactively from the command line or in a do-file. It looks like this: . input case expos pop 0 0 100 0 1 30 1 0 23 1 1 21 . end

You may also enter data directly in Stata's data window (not recommended; see section 2 and [GSW] 6, 9).

16

Reading ASCII data Reading tab- or comma-separated data

[R] insheet

In tab-separated data the values are separated by the tabulator character, here displayed as . A tab-separated ASCII file is created e.g. if you save an Excel worksheet as a text (.txt) file. If row 1 is variable names, Stata will find out and use them. In this and the following examples the value of type in observation 2 is missing. idtypesoldprice 124751.23 2 793199.70

You may read a tab-separated ASCII file with variable names in row 1 by the command: insheet using c:\dokumenter\p1\a.txt , tab

In comma-separated data each value is separated by a comma: 1,2,47,51.23 2,,793,199.70

If you have a comma-separated file without variable names in row 1 the command is: insheet id type sold price using c:\dokumenter\p1\a.txt , comma insheet assumes that all data belonging to one observation are in one line.

Reading freefield data

[R] infile (free format)

In freefield data each value is separated by commas or blanks: 1 2 47 51.23 2 . 793 199.70

If you have freefield data the command is infile id type sold price using c:\dokumenter\p1\a.txt infile does not assume that data belonging to one observation are in one line, and the

following data are the same as the data above: 1 2 47 51.23 2 . 793 199.70

Reading fixed format data

[R] infix; [R] infile (fixed format)

In fixed format data the information on each variable is determined by the position in the line. The blank type in observation 2 will be read as missing. 1 2 47 51.23 2 793 199.70

infix id 1 type 2-3 sold 4-7 price 8-14 using c:\dokumenter\p1\a.txt

Fixed format data can also be read by infile; to do this a dictionary file must be created, specifying variable names and positions etc. See [R] infile (fixed format).

17

9. Documentation commands

[GSW] 8

Stata does not need the documentation commands; you need the documentation yourself. You obtain that the output becomes more legible, and the risk of errors when interpreting the output is reduced.

Data set label

[U] 15.6.1; [R] label

You can give a short description of your data, to be displayed every time you open (use) data. label data "Fertility data Denmark 1997-1999. ver 2.5, 19.9.2000"

It is wise to include the creation date, to ensure that you analyse the most recent version.

Variable labels

[U] 15.6.2; [R] label

You can attach an explanatory text to a variable name. label variable q6 "Ever had itchy skin rash?"

Value labels

[U] 15.6.3; [R] label

You can attach an explanatory text to each code for a variable. This is a two-step procedure. First define the label (double quotes around text with embedded blanks): label define sexlbl

1 male

2 female

9 "sex unknown"

Next associate the label sexlbl with the variable sex: label values sex sexlbl [ , nofix ]

I prefer to add the nofix option; otherwise case listings can become quite clumsy. In output Stata unfortunately displays either the codes or the value labels, and you often need to see them together, to avoid mistakes. One solution is to include the code in the label: label define yesno 1 "1 yes" 2 "2 no"

Another solution is labjl, an .ado file developed by Jens Lauritsen, that incorporates the numerical code in the value label, so that both are displayed in tables. Find labjl.ado on the net with the Stata command: net search labjl or findit labjl. The program was published in STB (Stata Technical Bulletin) January 2001; this information should enable you to find and download it to c:\ado\stbplus. Most often you will use the same name for the variable and its label: label define sex 1 male 2 female label values sex sex [ , nofix ]

but the separate definition of the label enables you to reuse it: label define yesno 1 "1 yes" 2 "2 no" label values q1 yesno [ , nofix ] label values q2 yesno [ , nofix ]

18

If you want to correct a label definition or add new labels, use the modify option: label define sex

9 "unknown sex" , modify [ nofix ]

adds the label for code 9 to the existing label definition.

See label definitions See the value label definitions by: label list

the variable label definitions by: describe

and a full codebook by: codebook

Notes

[R] notes

You may add notes to your data set: note: 19.9.2000. Corrections made after proof-reading

and to single variables: note age: 20.9.2000. Ages > 120 and < 0 recoded to missing

The notes are kept in the data set and can be seen by: notes

19

10. Modifying data Don't misinterpret the title of this section: You should never modify your original data, but often you need to add modifications to a data set by generating new variables from the original data. Not documenting modifications may lead to serious trouble. Therefore modifications: • should always be made with a do-file with a name reflecting what it does: gen.alfa2.do generates alfa2.dta. • The first command in the do-file reads data (eg. use, infix). • The last command saves the modified data set with a new name (save). • The do-file should be 'clean', ie. not include commands irrelevant to the modifications. See examples of modifying do-files in section 16 and in Take good care of your data.

10.1. Calculations Operators in expressions Arithmetic

Relational

^ power

>

* multiplication

<

/ division

>=

+ addition

or equal < or equal equal not equal not equal

~ not ! not | or & and

Arithmetic operators Used for calculations. Examples: generate alcohol=beers+wines+spirits generate bmi=weight/(height^2)

The precedence order of arithmetic operators are as shown in the table; power before multiplication and division, before addition and subtraction. Control the order by parentheses: generate z= -(x+y^(x-y))/(x*y)

Relational and logical operators replace salary=. if agechi2 = 0.6984 Test that combined OR = 1: Mantel-Haenszel chi2(1) = Pr>chi2 =

5.85 0.0156

All procedures perform stratified analysis (Mantel-Haenszel). cc gives odds ratios for each stratum and the Mantel-Haenszel estimate of the common odds ratio, with confidence intervals. The test of homogeneity tests the requirement that the odds ratios don't differ 'too much' between strata. Command

Measure of association

Immediate command

ir

incidence rate ratio, incidence rate difference

iri

cs

risk ratio, risk difference

csi

cc

odds ratio

cci

tabodds

odds, several exposure levels. Trend test

mhodds

odds ratio, several exposure levels. Trend test

mcc

odds ratio (matched case-control data)

mcci

If you want to stratify by more than one variable the following command is useful: egen racesex=group(race sex) cc case exposed , by(racesex)

The immediate commands do not perform stratified analysis; an example with cci. Just enter the four cells (a b c d) of the 2×2 table: cci 10 20 17 9 , woolf

28

11.2. Continuous variables oneway

[R] oneway

Similar to the standard T-test, but compares more than two groups (analysis of variance): oneway price type [ , tabulate noanova] . oneway price type , tabulate type of | Summary of price per 75 cl bottle wine | Mean Std. Dev. Freq. ------------+-----------------------------------1 red | 48.15 12.650239 15 2 white | 42.590909 20.016952 11 3 rosé | 43. 45 17.3752 68 6 4 undeter | 59.616666 15.821924 3 ------------+-----------------------------------Total | 46.58 16.304104 35 Analysis of Variance Source SS df MS F Prob > F -----------------------------------------------------------------------Between groups 780.660217 3 260.220072 0.98 0.4162 Within groups 8257.34954 31 266.366114 -----------------------------------------------------------------------Total 9038.00975 34 265.823816 Bartlett's test for equal variances: chi2(3) = 2.3311

Prob>chi2 = 0.507

The table, but not the test, could also be obtained by; [R] tabsum

tabulate type , summarize(price)

anova

[R] anova

Similar to oneway, but handles a lot of complex situations.

table

[R] table

table is a flexible tool for displaying several types of tables, but includes no statistical tests.

To obtain the same table as in the oneway example: table type , content(n price mean price sd price)

Here comes the mean price distribution by two variables. The format option gives a nicer display. . table type rating , content(mean price) format(%9.2f) ---------------+------------------------------------------------------| quality rating type of wine | 1 poor 2 acceptable 3 good 4 excellent ---------------+------------------------------------------------------1 red | 51.20 41.45 52.09 34.95 2 white | 76.95 44.20 32.95 37.95 3 rosé | 72.95 45.95 40.95 4 undetermined | 55.95 45.95 76.95 ---------------+-------------------------------------------------------

29

ttest

[R] ttest

T-test for comparison of means for a continuous normally distributed variable (bmi) between two groups (sex): ttest bmi , by(sex) Standard t-test, equal variances assumed ttest bmi , by(sex) unequal Unequal variances assumed (see sdtest) ttest prebmi=postbmi Paired comparison of two variables ttest prebmi=postbmi , unpaired Unpaired comparison of two variables ttest bmidiff=0 One-sample t-test ttesti 32 1.35 .27 50 1.77 .33 Immediate command. Input n, mean and SD n1 m1 sd1 n2 m2 sd2 for each group

Distribution diagnostics Diagnostic plots:

[R] diagplots Normal distribution (P-P plot) Normal distribution (Q-Q plot) Formal test for normal distribution: [R] swilk swilk bmi Test for normal distribution Test for equal variances: [R] sdtest sdtest bmi , by(sex) Compare SD between two groups sdtest prebmi=postbmi Compare two variables Bartlett's test for equal variances is displayed by oneway, see above. pnorm bmi qnorm bmi

Non-parametric tests For an overview of tests avilable, in the Viewer window command line enter: search nonparametric

Here you see eg.: kwallis Kruskall-Wallis equality of populations rank test signrank Sign, rank, and median tests (Wilcoxon, Mann-Whitney)

11.3 Graphs

[GSW] 16; [G] (Graphics manual)

Stata's graphs are useful during analysis, but not for presentation (use eg. SigmaPlot). Modifying the looks of a graph is complicated; consult the Graphics manual [G]. If you installed StataQuest (see section 1.1) you can create a number of standard graphs using the menu system. Use xlab and ylab for axes except for categorical variables (sex). graph weight height , xlab ylab Scatterplot graph weight height age , matrix Scatterplot matrix graph weight , histogram normal xlab Histogram with normal curve hist agegrp , ylab Histogram, integer values generate x=1 graph x , bar by(sex) ylab graph height , bar means by(sex) ylab

30

Bars, count for each sex Bars, mean for each sex

12. Regression models Performing regression analysis with Stata is easy. Defining regression models that give sense is more complex. Especially consider: • If you look for causes make sure that your model is meaningful. Especially avoid to include variables that may represent steps in the causal pathway; it may create more confounding than it prevents. Automatic selection procedures are available in Stata (see [R] sw), but they may seduce the user to non-thinking. I will not describe them. • If your hypothesis is non-causal and you only look for predictors, logical requirements are more relaxed. But make sure you really are looking at predictors, not consequences of the outcome. • Take care with closely associated independent variables, e.g. education and social class. Including both may obscure more than illuminate.

12.1. Linear regression regress

[R] regress, [R] Regression diagnostics

A standard linear regression with bmi as the dependent variable: regress bmi sex age

xi:

[R] xi

The xi: prefix handles categorical variables in regression models. From a five-level categorical variable xi: generates four indicator variables; in the regression model they are referred to by the i. prefix to the original variable name: xi: regress bmi sex i.agegrp

By default the first (lowest) category will be omitted, i.e. be the reference group. You may, before the analysis, select agegrp 3 to be the reference by defining a 'characteristic': char agegrp[omit] 3

You may also use xi: to include interaction terms: xi: regress bmi age i.sex i.treat i.treat*i.sex

predict

[R] predict

After a regression analysis you may generate predicted values from the regression coefficients, and this may be used for studying residuals: regress bmi sex age predict pbmi generate rbmi = bmi-pbmi graph rbmi pbmi

31

The new variable pbmi is the bmi value predicted from the sex and age coefficients from the preceding regression analysis, and rbmi is the residual (observed minus predicted) to be plotted against pbmi, the predicted value. You may, however, use predict to generate the residual directly: predict pbmi predict rbmi , residual

You may also use coefficients generated from one sample to predict values in another sample.

12.2. Logistic regression logistic

[R] logistic

A standard logistic regression with ck as the dependent variable: logistic ck sex smoke speed alc

The dependent variable (ck) must be coded 0/1 (no/yes). If the independent variables are also coded 0/1 the interpretation of odds ratios is straightforward, otherwise the odds ratios must be interpreted per unit change in the independent variable. The xi: prefix applies as described in section 12.1: xi: logistic ck sex i.agegrp i.smoke xi: logistic ck i.sex i.agegrp i.smoke i.sex*i.smoke

After running logistic, use predict as described in section 12.1: predict

After running logistic obtain Hosmer-Lemeshow's goodness-of-fit test with 10 groups: lfit , group(10)

After running logistic obtain a classification table, including sensitivity and specificity with a cut-off point of your choice: lstat , cutoff(0.3)

Repeat lstat with varying cut-off points or, smarter, use lsens to see sensitivity and specificity with varying cutoff points: lsens

See a ROC curve: lroc

32

13. Survival analysis and related issues st

[R] st

The st family of commands includes a number of facilities, described on 200 pages in [R]. Here I describe the stset and stsplit commands and give a few examples. The data is cancer1.dta, a modification of the cancer.dta sample data accompanying Stata. The observation starts at randomization (agein), the data set includes these variables: . describe ------------------------------------------------------------------------storage display value variable name type format label variable label ------------------------------------------------------------------------lbnr byte %3.0f Patient ID drug byte %1.0f drug Drug type (1=placebo) drug01 byte %1.0f drug01 Drug: placebo or active agein float %6.3f Age at randomization ageout float %6.3f Age at death or censoring risktime float %6.3f Years to death or censoring died byte %1.0f died 1 if patient died ------------------------------------------------------------------------. summarize Variable | Obs Mean Std. Dev. Min Max -------------+----------------------------------------------------lbnr | 48 24.5 14 1 48 drug | 48 1.875 .8410986 1 3 drug01 | 48 .5833333 .4982238 0 1 agein | 48 56.328 5.659862 47.97637 67.73915 ageout | 48 57.61966 5.444583 49.87939 68.70284 risktime | 48 1.291667 .8546908 .0833333 3.25 died | 48 .6458333 .4833211 0 1

stset

[R] stset

stset declares the data in memory to be survival time (st) data. I create two versions: In st.cancer1.dta time simply is risktime, age not taken into consideration. In st.cancer2.dta time at risk is defined by age at entry (agein) and exit (ageout) enabling

to study and control for the effect of age.

Simple analysis – age not included stset data with risktime as the time-of-exit variable: . * c:\dokumenter\proj1\gen.st.cancer1.do . use c:\dokumenter\proj1\cancer1.dta , clear . stset risktime , failure(died==1) id(lbnr)

33

id: lbnr failure event: died == 1 obs. time interval: (risktime[_n-1], risktime] exit on or before: failure ------------------------------------------------------------------------48 total obs. 0 exclusions ------------------------------------------------------------------------48 obs. remaining, representing 48 subjects 31 failures in single failure-per-subject data 62 total analysis time at risk, at risk from t = 0 earliest observed entry t = 0 last observed exit t = 3.25 . summarize Variable | Obs Mean Std. Dev. Min Max -------------+----------------------------------------------------lbnr | 48 24.5 14 1 48 .... | risktime | 48 1.291667 .8546908 .0833333 3.25 died | 48 .6458333 .4833211 0 1 _st | 48 1 0 1 1 _d | 48 .6458333 .4833211 0 1 _t | 48 1.291667 .8546908 .0833333 3.25 _t0 | 48 0 0 0 0 . save c:\dokumenter\proj1\st.cancer1.dta

Four new variables were created, and the st'ed data set is prepared for a number of incidence rate and survival analyses: _st 1 if the observation includes valid survival time information, otherwise 0 _d 1 if the event occurred, otherwise 0 (censoring) _t time or age at observation end (here: risktime) _t0 time or age at observation start (here: 0) Summary of time at risk and incidence rates

[R] st stptime

stptime , by(drug) per(1000) dd(4) stptime , at(0(1)5) by(drug)

/* rates x 1000, 4 decimals */ /* 1-year intervals */

A table of the survivor function:

[R] st sts list

sts list , by(drug) compare at(0(0.5)5)

/* ½ year intervals */

The corresponding graph:

[R] st sts graph

sts graph , by(drug)

A logrank test comparing two or more groups:

[R] st sts test

sts test drug

Cox proportional hazards regression analysis: stcox drug01 xi: stcox i.drug

/* drug dichotomized */ /* 3 drugs */

34

[R] st stcox

Including age in the analysis stset data with ageout as the time-of-exit variable, agein as the time-of-entry variable: . . . .

* c:\dokumenter\proj1\gen.st.cancer2.do use c:\dokumenter\proj1\cancer1.dta , clear stset ageout , enter(time agein) failure(died==1) id(lbnr) summarize

Variable | Obs Mean Std. Dev. Min Max -------------+----------------------------------------------------lbnr | 48 24.5 14 1 48 .... | _st | 48 1 0 1 1 _d | 48 .6458333 .4833211 0 1 _t | 48 57.61966 5.444583 49.87939 68.70284 _t0 | 48 56.328 5.659862 47.97637 67.73915 . save c:\dokumenter\proj1\st.cancer2.dta

Summary of time at risk and incidence rates

[R] st stptime

stptime , at(45(5)70) by(drug)

/* 5 year age intervals */

The sts and stcox analyses as shown above now must be interpreted as age-adjusted (delayed entry analysis). [R] st stsplit

stsplit

To look at the influence of age at incidence or survival, stsplit the data, expanding each observation to an observation for each age interval: . . . .

* c:\dokumenter\proj1\gen.stsplit.cancer2.do use c:\dokumenter\proj1\st.cancer2.dta , clear stsplit agegr , at(45(5)70) summarize

Variable | Obs Mean Std. Dev. Min Max -------------+----------------------------------------------------lbnr | 61 26.37705 14.03586 1 48 drug | 61 1.967213 .8557105 1 3 drug01 | 61 .6229508 .4886694 0 1 agein | 61 55.58628 5.651005 47.09552 67.87458 ageout | 61 56.87054 5.563221 49.01218 68.8737 risktime | 61 1.412568 .8856398 .0833333 3.25 died | 48 .6458333 .4833211 0 1 _st | 61 1 0 1 1 _d | 61 .5081967 .5040817 0 1 _t | 61 56.87054 5.563221 49.01218 68.8737 _t0 | 61 55.85415 5.610502 47.09552 67.87458 agegr | 61 54.01639 5.832357 45 65 . save c:\dokumenter\proj1\stsplit.cancer2.dta

The data now look like this with 61 observations. Some of the original observations spanned over two age intervals. Events and risktime are distributed to the proper age intervals. Describe risktime etc. by: bysort drug: stsum , by(agegr)

35

poisson

[R] poisson

The stsplit.cancer2.dta data set above can be used for Poisson regression with a little more preparation. died and risktime must be replaced as shown. You also may collapse the file to a table with one observation for each age group and drug (see section 10.6): . . . . .

* c:\dokumenter\proj1\gen.stcollaps.cancer2.do use c:\dokumenter\proj1\stsplit.cancer2.dta , clear replace died = _d replace risktime = _t - _t0 summarize

Variable | Obs Mean Std. Dev. Min Max -------------+----------------------------------------------------.... | risktime | 61 1.016394 .7343081 .0833321 2.75 died | 61 .5081967 .5040817 0 1 _st | 61 1 0 1 1 _d | 61 .5081967 .5040817 0 1 _t | 61 56.87054 5.563221 49.01218 68.8737 _t0 | 61 55.85415 5.610502 47.09552 67.87458 agegr | 61 54.01639 5.832357 45 65 . collapse (sum) risktime died , by(agegr drug) . summarize Variable | Obs Mean Std. Dev. Min Max -------------+----------------------------------------------------drug | 15 2 .8451543 1 3 agegr | 15 55 7.319251 45 65 risktime | 15 4.133334 3.01067 .3581161 10.88906 died | 15 2.066667 2.374467 0 8 . save c:\dokumenter\proj1\stcollaps.cancer2.dta

These data are ready for a Poisson regression: . xi: poisson died i.drug i.agegr if risktime>0 , exposure(risktime) irr i.drug i.agegr

_Idrug_1-3 _Iagegr_45-65

(naturally coded; _Idrug_1 omitted) (naturally coded; _Iagegr_45 omitted)

Poisson regression

Number of obs = 15 LR chi2(6) = 29.20 Prob > chi2 = 0.0001 Log likelihood = -19.558255 Pseudo R2 = 0.4274 ------------------------------------------------------------------------died | IRR Std. Err. z P>|z| [95% Conf. Interval] -------------+----------------------------------------------------------_Idrug_2 | .2125451 .1044107 -3.15 0.002 .0811534 .5566669 _Idrug_3 | .1434259 .068503 -4.07 0.000 .0562441 .3657449 _Iagegr_50 | 2.427286 2.576403 0.84 0.403 .3031284 19.43637 _Iagegr_55 | 3.892978 4.067407 1.30 0.193 .5022751 30.17327 _Iagegr_60 | 6.20448 6.644274 1.70 0.088 .7606201 50.61077 _Iagegr_65 | 11.05612 12.48911 2.13 0.033 1.208027 101.1879 risktime |(exposure) -------------------------------------------------------------------------

After running poisson, test goodness-of-fit by: poisgof

36

14. Combining files

[U] 25

14.1. Appending files

[R] append

To combine the information from two files with the same variables, but different persons: * c:\dokumenter\proj1\gen.filab.do use c:\dokumenter\proj1\fila.dta , clear append using c:\dokumenter\proj1\filb.dta save c:\dokumenter\proj1\filab.dta

14.2. Merging files

[R] merge

To combine the information from two files with different information about the same persons: * c:\dokumenter\proj1\gen.filab.do use c:\dokumenter\proj1\fila.dta , clear merge lbnr using c:\dokumenter\proj1\filb.dta save c:\dokumenter\proj1\filab.dta

Both files must be sorted beforehand by the matching key (lbnr in the example above), and the matching key must have the same name in both data sets. Apart from the matching key the variable names should be different. Below A and B symbolizes the variable set in the two input files while numbers represent the matching key. Missing information is shown by . (period): fila

filb

filab

_merge

1A 2A

1B

1AB 2A. 3.B 4A1B 4A2B

3 1 2 3 3

4A1 4A2

3B 4B

Stata creates the variable _merge which takes the value 1 if only data set 1 (fila) contributes, 2 if only data set 2 (filb) contributes, and 3 if both sets contribute. Check for mismatches by: tab1 _merge list lbnr _merge

if

_merge < 3

For lbnr 4 there were two observations in fila, but only one in filb. The result was two observations with the information from filb assigned to both of them. This enables to distribute information eg. about doctors to each of their patients – if that is what you desire. But what if the duplicate lbnr 4 was an error? To check for duplicate id's before merging, compare with the previous observation: sort lbnr list lbnr if lbnr==lbnr[_n-1]

Use collapse (see section 10.6) if you want to aggregate eg. patient information before merging with information about doctors. 37

15. Miscellaneous 15.1. Memory considerations

[U] 7

A Stata data set can have a maximum of 2,047 variables. Stata keeps the entire data set in memory, and the number of observations is limited by the memory allocated. The memory must be allocated before you open (use) a data set. As described in section 1.2 the initial default memory is defined in the Stata icon. To change this to 15 MB, right-click the icon, select Properties, and change the path field text to: c:\stata\wstata.exe /m15. If the memory allocated is insufficient you get the message: no room to add more observations

You may increase the current memory to 25 MB by: clear /* You can't change memory size with data in memory */ set memory 25m

You can see the amount of used and free memory by: memory

compress

[R] compress

To reduce the physical size of your data set – and the memory requirements – Stata can find out to use the fewest bytes needed for each variable (see section 6.2), and you can safely issue: compress

and save the data set again (save... , replace). This can reduce the amount of memory needed by 80%.

Handling huge data sets If you are regularly handling huge data sets you may consider: • to use compress to reduce memory requirements • to increase your computer's RAM (not very expensive) • to purchase Stata/SE (special edition allowing up to 32,000 variables) You might not be able to create the entire Stata data set because of its hugeness, but after compression you can handle it. However, you can not compress a Stata data set before it is created. Try to read one part of the data, compress and save, read the next part of the data, compress and save, etc., and finally combine (append) the partial data sets (see section 14.1): * c:\dokumenter\p1\gen.aa.do infix id 1 type 2 sold 4-7 using c:\dokumenter\p1\aa.txt in 1/10000 compress save c:\tmp\aa1.dta infix id 1 type 2 sold 4-7 using c:\dokumenter\p1\aa.txt in 10001/20000 compress save c:\tmp\aa2.dta append using c:\tmp\aa1.dta save c:\dokumenter\p1\aa.dta

38

15.2. String variables

[U] 15.4; [U] 26

Throughout this text I have demonstrated the use of numeric variables, but Stata also handles string (text) variables. It is almost always easier and more flexible to use numeric variables, but sometimes you might need string variables. A string can include any character, also numbers; however numbers are not interpreted by their numeric value, just as a sequence of characters. The relational operators > and < have no meaning while == and ~= do have meaning. String values must be enclosed in quotation marks: replace ph=45 if nation == "Danish"

Note that "Danish", "danish", and "DANISH" are different string values.

String formats

[U] 15.5.5

%10s displays a 10 character string, right-justified; %-10s displays it left-justified.

Reading string variables into Stata In the commands reading ASCII data (see section 8) the default data type is numeric. String variables should be defined in the input command. str5 means a 5 character text string: infix id 1-4 str5 icd10 5-9 using c:\dokumenter\p1\a.txt

Generating new string variables The first time a string variable is defined it must be declared by its length (str10): generate str10 nation = "Danish" if ph==45 replace nation = "Swedish" if ph==46

Conversion between string and numeric variables Number strings to numbers If a CPR number is recorded in cprstr (type string), no calculations can be performed. Conversion to a numeric variable cprnum can be obtained by: generate double cprnum = real(cprstr) format cprnum %10.0f cprnum is a 10 digit number and must be declared double for precision (see section 6.2).

Another option is destring (it automatically declares cprnum double): destring cprstr , generate(cprnum)

Non-number strings to numbers If a string variable sex is coded as eg. "M" and "F", convert to a numeric variable gender (with the original string codes as value labels) by: encode sex , generate(gender) [R] encode Display the meaning of the numeric codes by: label list gender

39

Numbers to strings You want the numeric variable cprnum converted to a string variable cprstr: generate str10 cprstr = string(cprnum , "%10.0f")

String manipulations

[U] 16.3.5; [GSW] 12

Strings can be combined by +: generate str5 svar5 = svar3 + svar2

You may isolate part of a string variable by the substr function. The arguments are: source string, start position, length. In the following a3 will be characters 2 to 4 of strvar: generate str3 a3 = substr(strvar,2,3)

You may substitute characters within a string. In an example above the string variable cprstr was created from the numeric variable cprnum. However, for persons with a leading 0 in the CPR number the string will start with a blank, not a 0. This can be remedied by: replace cprstr = subinstr(cprstr," ","0",1)

The upper function converts lower case to upper case characters; the lower function does the opposite. Imagine that ICD-10 codes had been entered inconsistently, the same code somtimes as E10.1, sometimes as e10.1. These are different strings, and you want them to be the same (E10.1): replace icd10 = upper(icd10)

Handling complex strings, eg. ICD-10 codes

[U] 26.4

In the ICD-10 classification of diseases all codes are a combination of letters and numbers (eg. E10.1 for insulin demanding diabetes with ketoacidosis). This is probably convenient for the person coding diagnoses (an extremely important consideration), but for the data handling it is quite inconvenient. My suggestion is to split a 5 character ICD-10 string variable (scode) into a one character string variable (scode1) and a four digit numeric variable (ncode4): generate str1 scode1 = substr(scode,1,1) generate ncode4 = real(substr(scode,2,4)) format ncode4 %4.1f

What did we obtain? Two variables: the string variable scode1 with 26 values (A to Z) and a numeric variable ncode4 (0.0-99.9). Now identify diabetes (E10.0-E14.9) by: generate diab=0 replace diab=1 if scode1=="E" & ncode4>=10 & ncode4=4 & byear =5 & pos7 =58 replace byear = 100*century + byear generate bdate = mdy(bmon,bday,byear)

The information on sex can be extracted from control; the mod function calculates the remainder after division by 2 (male=1, female=0): generate sex = mod(control,2)

Century information in CPR numbers The 7th digit (the first control digit) informs on the century of birth: Pos. 5-6 (year of birth) Pos. 7

00-36

37-57

58-99

0-3 4,9 5-8

19xx 20xx 20xx

19xx 19xx not used

19xx 19xx 18xx

Source: www.cpr.dk

Validation of CPR numbers To do the modulus 11 test for Danish CPR numbers first multiply the digits by 4, 3, 2, 7, 6, 5, 4, 3, 2, 1; next sum these products; finally check whether the sum can be divided by 11. Assume that the CPR numbers were split into 10 one-digit numbers c1-c10. Explanation of for : see section 7. generate test=0 for C in varlist c1-c10 \ X in numlist 4/2 7/1 : replace test=test+C*X replace test=mod(test,11) list id cpr test if test ~=0

To extract c1-c10 from the string cprstr: for C in newlist c1-c10\X in numlist 1/10 : /* */ gen C=real(substr(cprstr,X,1))

To extract c1-c10 already when reading data: infix str10 cprstr 1-10 c1-c10 1-10 using c:\dokumenter\p1\dfile.txt

With the techniques shown I developed an ado-file (cprcheck.ado) that extracts birth date and sex information and checks the validity of a CPR number. Download cprcheck.ado from www.svf-it.au.dk (not yet in place) to c:\ado\personal; see section 15.7. 42

15.4. Random samples, simulations Random number functions

[U] 16.3.2

Stata can create 'pseudo-random' numbers: generate y=uniform() generate y=invnorm(uniform()) generate y=10+2*invnorm(uniform())

Uniformly distributed in the interval 0-1 Normal distribution, mean=0, SD=1 Normal distribution, mean=10, SD=2

If you run the same command twice it will yield different numbers. If you need to reproduce the same series of 'random' numbers, initialize the seed (a large integer used for the initial calculations): set seed 83750971

Random samples and randomization You may use sample to select a random sample of your data set: sample 10 Selects an approximately 10 percent random sample. You may assign cases randomly to two treatments: generate y=uniform() generate treat=1 replace treat=2 if y>0.5

And you may sort your observations in random sequence: generate y=uniform() sort y

Generating artifical data sets You may use set obs to create empty cases. The following sequence defines a file with 10,000 cases, used to study the behaviour of the difference (dif) between two measurements (x1 x2), given information about components of variance (sdwithin sdbetw). set obs 10000 generate sdbetw = 20 generate sdwithin = 10 generate sdtotal = sqrt(sdbetw^2 + sdwithin^2) generate x0 = 50 + sdbetw*invnorm(uniform()) generate x1 = x0 + sdwithin*invnorm(uniform()) generate x2 = x0 + sdwithin*invnorm(uniform()) generate dif = x2 - x1 summarize

43

15.5. Immediate commands

[U] 22

An 'immediate' command requires tabular or aggregated input; data in memory are not affected. The immediate commands tabi, cci, csi, iri and ttesti are mentioned in section 11, and sampsi (sample size estimation) in section 15.6.

Confidence intervals

[R] ci

The general command ci and the 'immediate command cii calculate confidence intervals. I here show the use of cii: Normal distribution: cii 372 37.58 16.51 N

Binomial distribution: Poisson distribution:

cii cii

mean

153

40

N

events

247.1 T

SD

40 , poisson events

If you installed StataQuest, you may use the menus (calculator) to generate these commands.

Stata as a pocket calculator

[R] display

The display command gives the opportunity to perform calculations not affecting the data in memory (_pi is a Stata constant): . display 2*_pi*7 43.982297

You may include an explanatory text: . display "The circumference of a circle with radius 7 is " 2*_pi*7 The circumference of a circle with radius 7 is 43.982297

44

15.6. Sample size and study power sampsi

[R] sampsi

Sample size and study power estimation are pre-study activities: What are the consequences of different decisions and assumptions for sample size and study power? You must make these decisions: • The desired significance level (alpha). Default: 0.05. • The minimum relevant contrast – expressed as study group means or proportions. • Sample size estimation: The desired power. Default: 0.90. • Power estimation: Sample sizes. And with comparison of means you must make an assumption: • The assumed standard deviation in each sample. Here are minimum examples for the four main scenarios: Comparison of:

Sample size estimation

Power estimation

Proportions

sampsi 0.4 0.5

sampsi 0.4 0.5 , n(60)

Means

sampsi 50 60 , sd(8)

sampsi 50 60 , sd(8) n(60)

Further options are available: Sample size Situation

Option

Significance level (default: 0.05)

Power

prop.

mean

prop.

mean

alpha(0.01)

+

+

+

+

Power (default: 0.90)

power(0.95)

+

+

Unequal sample sizes, ratio=n2/n1

ratio(2)

+

+

Unequal sample sizes

n1(40) n2(80)

+

+

Unequal SDs

sd1(6) sd2(10)

+

+

Example: Sample size estimation for comparison of means, unequal SDs and sample sizes: sampsi 50 60 , sd1(14) sd2(10) ratio(2) sampsi also handles trials with repeated measurements, see the Reference manual.

45

15.7. ado files

[U] 20-21, [P] (Programming manual)

An .ado file is a program. Most users will never write programs themselves, but just use existing programs. Read more in the User's Guide ([U] 20-21) and the programming manual [P]. Save user-written programs in c:\ado\personal. To see the locations of all .ado files issue the command sysdir. The simplest form of an .ado file is a single command or a do-file with a leading program define command and a terminating end command. Here are a few examples to demonstrate that creating your own commands is not that impossible. Two pitfalls: 1) In the manuals single quotes are shown as ` and ', but actually the ending quote must be the simple '. 2) There must be a blank line after the terminating end.

time displays date and time program define time * c:\ado\personal\time.ado. Displays date and time. display " $S_DATE $S_TIME " end

tabx for many two-way tables I want 10 two-way tables: each of the variables q1-q10 by treat. With tabulate I must issue 10 commands. If I call tab2 with all 11 variables I get 55 two-way tables with all possible combinations of the 11 variables. I therefore created tabx.ado, in this instance to be called by: tabx (q1-q10)(treat) , chi2. Following the program I illustrate the substitutions that take place while executing the program. Note the extensive use of indentations and comments. They are not required by Stata, but they may help the reader understand what happens. *! version 1.1. 28mar2001 *! Two-way tables combining each element in (rowvarlist) with *! each element in (colvarlist). tabulate options apply. *! Sample call: tabx (q1-q10) (treat) , chi2 *! Author: Svend Juul program define tabx gettoken row rest : 0 , match(dummy) /* row = row varlist */ gettoken col rest : rest , match(dummy) /* col = column varlist */ /* rest = the remainder */ foreach r of varlist `row' { /* loop: each row variable local rl : variable label `r' /* rl = row variable label foreach c of varlist `col' { /* loop: each col variable local cl : variable label `c' /* cl = col variable label display " " /* show var names & labels display "Rows: `r' `rl' Columns: `c' `cl' " tabulate `r' `c' `rest' /* tabulation */ } /* end col loop */ } /* end row loop */ end

46

*/ */ */ */ */

The gettoken commands parse (analyse) the input: gettoken row rest : 0 , match(dummy) 0 denotes everything after the program name; 0='(q1-q10)(treat), chi2'; it is split in

two: row='q1-q10' and rest='(treat), chi2'. The second gettoken command: gettoken col rest : rest , match(dummy)

splits rest in two: col='treat' and rest=', chi2'. Now the following commands are created (only the main commands are shown): foreach r of varlist q1–q10 { foreach c of varlist treat tabulate `r' `c' , chi2 } }

{

The two foreach loops create 10 tabulate commands: tabulate q1 treat , chi2 tabulate q2 treat , chi2

etc. The two local commands identify the variable labels to be printed with each table (the display command). Two ado-files useful for the interaction between Stata and NoteTab are shown in appendix 3.

47

15.8. Exchange of data with other programs Beware: Translation between programs may go wrong, and you should check carefully eg. by comparing the output from SPSS' DESCRIPTIVES and Stata's summarize. Especially compare the number of valid values for each variable and take care with missing values and date variables.

StatTransfer

[U] 24.4

StatTransfer is a reasonably priced program (purchase: see Appendix 1) that translates between a number of statistical packages, including Stata. Variable names, and variable and value labels are transferred too. A previous version of StatTransfer understood Stata 6.0, but not 7.0 data sets. To create a Stata 6.0 data set for conversion: save c:\dokumenter\proj1\alfa.dta , old

Transferring data to Excel and other spreadsheets

[R] outsheet

Probably all statistical packages read Excel data. To create a tab-separated file (see section 8) readable by Excel: outsheet [varlist] using c:\dokumenter\proj1\alfa.txt , nolabel In Excel open the file as a text file and follow the instructions. Variable names, but no labels are transferred. [R] outfile If you want the data written to a comma-separated ASCII file the command is: outfile [varlist] using c:\dokumenter\proj1\alfa.txt , nolabel comma

Reading Excel data

[R] insheet

Most packages can create Excel data and probably all can create textfiles similar to those created by Stata's outsheet command. From Excel save the file as a tab-separated text file (see section 8). Stata reads it by: insheet using c:\dokumenter\p1\a.txt , tab

15.9. For old SPSS users SPSS and Stata have similarities and differences. Among the differences are: • Stata does not have user-defined missing values; see section 6.3 and Take Good care of your data. • Stata's missing value is a high number. This may complicate conditions; see section 6.2. • While SPSS executes all transformation commands up to a procedure command one case at a time, Stata performs each command for the entire data set before proceeding to the next command. This may affect the result, especially when combining selections (drop if...) with lagged observations ([_n-1]). 48

Frequently used SPSS commands and the similar Stata commands SPSS command

Similar Stata command

DATA LIST

infile; infix; insheet

GET FILE

use

SAVE OUTFILE

save

VARIABLE LABELS

label variable

VALUE LABELS

label define

followed by

label values FORMATS sex (F1)

format sex %1.0f

MISSING VALUES

No user-defined missing values in Stata

COMMENT

comment

DOCUMENT

note

COMPUTE

generate; replace; egen

IF (sex=1) y=2

generate y=2 if sex==1

RECODE agegr (90 THRU 120 = 90)

recode agegr 90/120=90

DO REPEAT...END REPEAT

for

SELECT IF

keep if;

SAMPLE 0.1

sample 10

SPLIT FILE

by...: ;

WEIGHT

Weights can be included in most commands

DISPLAY DICTIONARY

describe; codebook

DESCRIPTIVES

summarize

FREQUENCIES

tab1

CROSSTABS

tabulate; tab2

MEANS bmi BY agegrp

oneway bmi agegrp , tabulate

T-TEST

ttest

LIST

list

WRITE

outfile; outsheet

SORT CASES BY

sort

AGGREGATE

collapse

ADD FILES

append

MATCH FILES

merge

49

or *

drop if

bysort...:

16. Do-file examples Here follow short examples of do-files doing typical things. Find more examples in Take good care of your data. All major work should be done with do-files rather than by entering single commands because: 1. The do-file serves as documentation for what you did. 2. If you discover an error you can easily correct it and re-run the do-file. 3. You are certain that commands are executed in the sequence intended. Example 1 generates the first Stata version of the data, and example 2 generates a modified version. I call both do-files vital in the sense that they document modifications to the data. Such do-files are part of the documentation and the should be stored safely. Safe storage also means safe retrieval, and they should have names telling what they do. My own principle is this: In example 1 gen.wine.do generates wine.dta. In example 2 gen.visit12a.do generates visit12a.dta. This is different from example 3 where no new data are generated, only output. This do-file is not vital in the same sense as example 1 and 2, and it should not have the gen. prefix (the Never Cry Wolf principle). Example 1. gen.wine.do generates Stata data set wine.dta from ASCII file. * gen.wine.do creates wine.dta 13.5.2001 infix id 1-3 type 4 price 5-10 rating 11 /* */ using c:\dokumenter\wines\wine.txt * Add variable labels label variable id "Identification number" lab var type "Type of wine" lab var price "Pri ce per 75 cl bottle" lab var rating "Quality rating" * Add value labels label define type 1 "red" 2 "white" 3 "rosé" 4 "undetermined" label values type type lab def rating 1 "poor" 2 "acceptable" 3 "good" 4 "excellent" lab val rating rating * Add data set label label data "wine.dta created from wine.txt, 13.5.2001" save c:\dokumenter\wines\wine.dta

Example 2. gen.visit12a.do generates visit12a.dta from visit12.dta * gen.visit12a.do generates visit12a.dta with new variables. use c:\dokumenter\proj1\visit12.dta, clear * Calculate hrqol: quality of life score. egen hrqol=rsum(q1-q10) label variable hrqol "Quality of life score" label data "Visit12a.dta created by gen.visit12a.do, 02.01.2001" save c:\dokume nter\proj 1\visit12 a.dta

50

Example 3. Analyse Stata data * winedes.do Descriptive analysis of the wine data use c:\dokumenter\wines\wine.dta describe codebook summarize tab1 type rating tabulate type rating , chi2 exact oneway price rating , tabulate

14.5.2001

Note that in example 1 and 2 the structure was: 1. Read data (infix, use) 2. Calculation and documentation commands 3. Save data (save) In example 3 the structure was: 1. Read data (use) 2. Analysis commands

Example 4. Elaborate profile.do * c:\dokumenter\profile.do

executes automatically when opening Stata

* Activate StataQuest menus (provided you installed StataQuest) quest on * Write session start time in tid.do set obs 1 generate str70 tid = "***** Session started ***** outfile tid using c:\tmp\tid.do , noquote replace clear

$S_DATE $S_TIME"

* Copy session start time to the cmdlog (cmdlog.txt) and open it. * ! in front: Stata interprets the following as a DOS command ! IF EXIST c:\tmp\cmdlog.old DEL c:\tmp\cmdlog.old ! RENAME c:\tmp\cmdlog.txt cmdlog.old ! COPY /B c:\tmp\cmdlog.old + c:\tmp\tid.do c:\tmp\cmdlog.txt cmdlog using c:\tmp\cmdlog.txt , append * Open the log (stata.log) and write session start time to it. set logtype text log using c:\tmp\stata.log , replace noisily do c:\tmp\tid.do

Compared to the profile.do suggested in section 1.2, this version adds a time stamp to the lean log file (cmdlog.txt). This means better possibilities to reconstruct previous work.

51

Appendix 1

Purchasing Stata and manuals To most users the following manuals (by Metrika termed "Mellempakken") will suffice: 1. Getting Started manual [GSW] 2. User's Guide [U] 3a. Reference Manual Extract [R] (one volume) Ambitious users and those who want explanation and documentation of the statistical procedures used should purchase the full package with 3b instead of 3a: 3b. Reference Manual [R] (four volumes) Other manuals are: 4. Programming [P] 5. Graphics [G] The Scandinavian sales agent for Stata and StatTransfer is Metrika (www.metrika.se). Students and employees at University of Aarhus and Aarhus University Hospital can purchase Stata at a special discount rate. Other educational institutions may have similar arrangements. Information about the procedure can be found at www.svf-it.au.dk.

Other material Jens Lauritsen wrote an instructive Danish introduction to Stata; download it for free from www.bola.suite.dk/stata/statanot.pdf. Also look at www.stata.com. They inform not only about their own products but also about third party books etc. We plan to use www.svf-it.au.dk for exercise data and other material.

52

Appendix 2

EpiData 2.1

www.epidata.dk EpiData is an easy-to-use program for entering data. It has the facilities needed, but nothing superfluous. Data entered can be saved as EpiInfo, Excel, DBase, SAS, SPSS and Stata files. EpiData with documentation is available for free from http://www.epidata.dk.

EpiData files If your dataset has the name first, you will work with three files: first.qes is the definition file where you define variable names and entry fields. first.rec is the data file in EpiInfo 6 format. first.chk is the checkfile defining variable labels, legal values and conditional jumps.

Suggested options Before starting for the first time you should set general preferences (File < Options). I recommend: [Show dataform] Font: Back grou nd: Field colour: Active field: Entry field style: Line heigh t:

[Create datafile] Co urier N ew b old 10 pt. W hite Light blue Highlighted, yellow Flat with border 1

First word in question is fieldname (impo rtant!) Lowercase

Working with EpiData EpiData's toolbar guides you through the process: [Define data] º [Make datafile] º [Add checks] º [Enter data] º [Document] º [Export data]

[Define Data]. You get the EpiData editor where you define variable names, variable labels, and field formats. If the name of your dataset is first, save the definition file as first.qes: FIRST.QES

My first tr y with EpiD ata.

entrdate Date entered lbnr Questionnaire number #### init Initials ___ sex Sex # (1 male 2 female) npreg Number of pregnancies ## ========================================================= Page 2 bdate Date of birth occup Occupation ## (see coding instruction OCCUP)

The first word is the variable name, the following text becomes the variable label. ## indicates a two-digit numeric field, ##.# a four-digit field with one decimal, ___ a three character string variable, a date, and an automatic variable: the date of entering the observation. Variable names can have up to 8 characters a-z (but not æøå) and 09; they must start with a letter. Avoid special characters, also avoid _ (underscore). If you use Stata for analysis remember that Stata is case-sensitive (I recommend to always use lowercase variable names). Text not preceding a field definition ("1 male 2 female"; "======="; "Page 2") can be used for instructions etc. while entering data.

53

[Make Datafile]: Save the empty data file first.rec. The .rec files are in EpiInfo 6 format. [Add checks]: You do not have to write the actual code yourself, but may use the menu system. The information is stored in a checkfile (first.chk) which is structured as below. * FIRST.CHK

Good idea to include the checkfile name as a comment.

LABELBLOCK LABEL sexlbl 1 Male 2 Female END END

Create the label definition sexlbl . You might give it the name sex – but eg. a label definition n0y1 might define the code 0 for no and 1 for yes, to be used for several variables.

sex COMMENT LEGAL USE sexlbl JUMPS 1 bdate END END

Use the sexlbl label definition for sex . Other entries than 1, 2 and nothing will be rejected. If you enter 1 for sex you will jump to the variable bdate . If you use the menu to create the jump, enter 1>bdate .

The meaning of the Menu dialog box is not obvious at first sight, and I will explain a little: Checkfile name first.chk ?

Sex

Selec t the variable

Sex of respondent Num ber

Variable label and data type displayed

Range, Legal

1,2,9

Define po ssible va lues. A range eg. as: 0-10, 99

Jumps

1>b date

Jump to bdate if sex = 1

Must enter

No

?

Skipping the field may be prevented

Repeat

No

?

Same value inserted in all records (why use it?)

Value label

sexlbl

?

Save

Ed it

+

[ ?]: Select among existing label definitions [+]: D efine new value labels

Close

Save: Save variable definitions Ed it: Edit variable definitions

[Enter data]: You see a data entry form as you defined it; it is straightforward. With the options suggested the active field shifts colour to yellow, making it easy for you to see where you are. As an assurance against typing mistakes you may enter part or all of the data a second time in a second file and compare the contents of file1 and file2. [Document] lets you create a codebook, including variable and value labels and checking rules. The codebook shown below displays structure only, to be compared with your primary codebook; you also have the option to display information about the data entered. [Export]: Finally you can export your data to a statistical analysis programme. The .rec file is in EpiInfo 6 format, and EpiData creates Dbase, Excel, Stata, SPSS and SAS files. Variable and value labels are transferred to Stata, SAS and SPSS files, but not to spreadsheets.

54

Appendix 3

NoteTab Light

www.notetab.com The Results window has severe limitations, and the Viewer window has some limitations in the ability to handle the output. (Version 7 is much better at that than version 6, probably Stata will include a fully editable output window in the future). You will therefore benefit from a standard text editor. I use NoteTab Light, available for free from www.notetab.com. This delicious editor has several advantages over NotePad, Microsoft's standard text editor. I also find NoteTab superior to Stata's Do-file editor – but you cannot execute a do-file directly from NoteTab as you can from the Do-file editor. The use is straightforward, like a word processor. I recommend the following options: View < Printing Options Margins Font Other Page N um bers Num ber format Header Footer Date Filter

Left 2 cm, Right 1 cm, Top 1 cm , Bottom 1 cm Letter Gothic Bold 9 pt Top, right Pag e % d Date + T itle None "your name" dd.mm .yyyy hh.nn

When you finished, save the settings by clicking the [Save] button.

Making NoteTab work with Stata In the following I assume that you in profile.do (see section 1.2) defined c:\tmp\stata.log as the full log, including both commands and results; it must be in simple text format, not SMCL format. Open c:\tmp\stata.log in NoteTab to view, edit and print results. In NoteTab each file has it own tab; you need not close them at exit. If NoteTab was open while running Stata you might not see the latest output, but don't worry, just select Edit < Reload (or [Alt]+[E] [R]), and you have access to the updated output. You may also right-click the file's tab and select Reload. I suggest that you create two ado-files (see section 15.7) to ease your work:

nt opens Stata's log in NoteTab Enter nt in Stata's command line window, and the log (c:\tmp\stata.log) opens in NoteTab. ! means that a Windows command follows: program define nt * c:\ado\personal\nt.ado. Opens the Stata log file in NoteTab. ! "C:\programmer\NoteTab Light\NoteTab.exe" c:\tmp\stata.log end

newlog discards old log and opens new log program define newlog * c:\ado\personal\newlog.ado. Discards old log and opens new log. log close log using c:\tmp\stata.log , replace end

55

Alphabetic index A ado files . . . . . . . . . . . . . . Aggregating data . . . . . . . anova . . . . . . . . . . . . . . . append . . . . . . . . . . . . . . Arithmetic operators . . . . ASCII data . . . . . . . . . . . .

46 24 29 37 20 17

E

L

egen . . . . . . . . . . . . . . . . 21 encode . . . . . . . . . . . . . . 39

label . . . . . . . . . . . . . . . 18

EpiData . . . . . . . . . . . . . . 53 epitab . . . . . . . . . . . . . . 28 Error messages . . . . . . . . . . 7 Excel . . . . . . . . . . . . . . . . 48

B

F

Bar graph . . . . . . . . . . . . . 30 Bartlett's test . . . . . . . . . . . 30 browse . . . . . . . . . . . . . . . 6 by prefix . . . . . . . . . . . . . 14 bysort prefix . . . . . . . . 14

File names . . . . . . . . . . . . . 9 findit . . . . . . . . . . . . 7 Fixed format data . . . . . . . 17 for . . . . . . . . . . . . . . . . . . 15 format . . . . . . . . . . . . . . 11 Format, dates . . . . . . . . . . 41 Format, numeric data . . . . 11 Format, strings . . . . . . . . . 39 Freefield data . . . . . . . . . . 17 fweight . . . . . . . . . . . . . 13 generate . . . . . . . . . . . . 21

C Calculations . . . . . . . . . . . 20 cc , cci . . . . . . . . . . . . . . 28 char . . . . . . . . . . . . . . . . 31 ci , cii . . . . . . . . . . . . . . 44 collapse . . . . . . . . . . . . 24 Combining files . . . . . . . . 37 Comma-separated data . . . 17 Command line window . . . 5 Command syntax . . . . . . . 12 Comments . . . . . . . . . . . . 14 compress . . . . . . . . . . . . 38 Conditional commands . . 13 Confidence intervals . . . . 44 Continuation lines . . . . . . 14 Cox regression . . . . . . . . . 34 CPR numbers . . . . . . . . . . 41 cs , csi . . . . . . . . . . . . . . 28 Customizing Stata . . . . . . . 3

D Data set label . . . . . . . . . . 18 Data window . . . . . . . . . . . 6 date function . . . . . . . . . 41 Date variables . . . . . . . . . 41 display . . . . . . . . . . . . . 44 do . . . . . . . . . . . . . . . . . . . . 8 Do-file editor . . . . . . . . . . . 6 Do-files . . . . . . . . . . . . 8, 50 drop . . . . . . . . . . . . . . . . 22

Labels . . . . . . . . . . . . . . . . lfit . . . . . . . . . . . . . . . . Linear regression . . . . . . . list . . . . . . . . . . . . . . . . Logical operators . . . . . . . logistic . . . . . . . . . . . . Logistic regression . . . . . . Logrank test . . . . . . . . . . . Long command lines . . . . lroc . . . . . . . . . . . . . . . . lstat . . . . . . . . . . . . . . .

M

help . . . . . . . . . . . . . . . . . 7

Mann-Whitney test . . . . . . Mantel-Haenszel analysis ...................... Manuals . . . . . . . . . . . . . . Matrix scatterplot . . . . . . . mcc , mcci . . . . . . . . . . . mdy function . . . . . . . . . . memory . . . . . . . . . . . . . . Memory considerations . . merge . . . . . . . . . . . . . . . mhodds . . . . . . . . . . . . . . Missing values . . . . . . . . . mvdecode . . . . . . . . . . . .

Histogram . . . . . . . . . . . . . 30 Hosmer-Lemeshow test . . 32

N

G Goodness-of fit test . . 32, 36 graph . . . . . . . . . . . . . . . 30 Graphs . . . . . . . . . . . . . . . 30

H

I if qualifier . . . . . . . . 13, 20

Immediate commands . . . 44 in qualifier . . . . . . . . . . . 13 infile . . . . . . . . . . . . . . 17 infix . . . . . . . . . . . . . . . 17 input . . . . . . . . . . . . . . . 16 insheet . . . . . . . . . . 17, 48 ir , iri . . . . . . . . . . . . . . 28

K Kaplan-Meier curve . . . . . keep . . . . . . . . . . . . . . . . Kruskall-Wallis test . . . . . kwallis . . . . . . . . . . . . .

56

34 22 30 30

18 32 31 25 20 32 32 34 14 32 32

30 28 52 30 28 41 38 38 37 28 11 11

newlog.ado (user program) . . . . . . . . . . . . . . . . . . . . . . 55 Non-parametric tests . . . . 30 Normal distribution . . . . . 30 Notation in this booklet . . . 2 note . . . . . . . . . . . . . . . . 19 NoteTab Light . . . . . . . . . 55 nt.ado (user program) . . . . 55 Number lists . . . . . . . . . . . 15 Numbering observations . 23 Numeric formats . . . . . . . 11 Numeric ranges . . . . . . . . 13 Numeric variables . . . . . . 10

O Observations . . . . . . . . . . oneway . . . . . . . . . . . . . . Operators . . . . . . . . . . . . . Options . . . . . . . . . . . . . . . order . . . . . . . . . . . . . . . outfile . . . . . . . . . . . . . outsheet . . . . . . . . . . . .

10 29 20 12 23 48 48

P P-P plot . . . . . . . . . . . . . . . 30 pnorm . . . . . . . . . . . . . . . 30 poisgof . . . . . . . . . . . . . 36 poisson . . . . . . . . . . . . . 36 Poisson regression . . . . . . 36 Power estimation . . . . . . . 45 predict . . . . . . . . . . . . . 31 profile.do . . . . . . . 4, 51 Programs . . . . . . . . . . . . . 46

Q Q-Q plot . . . . . . . . . . . . . . qnorm . . . . . . . . . . . . . . . Qualifiers . . . . . . . . . . . . . Quotes . . . . . . . . . . . . . . .

30 30 12 14

R Random numbers . . . . . . . 43 Random samples . . . . . . . 43 real function . . . . . . . . . 39 recode . . . . . . . . . . . . . . 22 regress . . . . . . . . . . . . . 31 Regression, Cox . . . . . . . . 34 Regression, linear . . . . . . . 31 Regression, logistic . . . . . 32 Regression, Poisson . . . . . 36 Relational operators . . . . . 20 rename . . . . . . . . . . . . . . 23 Reordering variables . . . . 23 replace . . . . . . . . . . . . . 21 Results window . . . . . . . . . 5 Review window . . . . . . . . . 5 ROC curve . . . . . . . . . . . . 32 run . . . . . . . . . . . . . . . . . . . 8

S

U

sample . . . . . . . . . . . 23, 43

update query . . . . . . . . 4 use . . . . . . . . . . . . . . . . . . 16

Sample size estimation . . . 45 sampsi . . . . . . . . . . . . . . 45 save . . . . . . . . . . . . . . . . 16 Scatterplot . . . . . . . . . . . . 30 sdtest . . . . . . . . . . . . . . 30 search . . . . . . . . . . . . . . . 7 Selecting observations . . . 22 Selecting variables . . . . . . 23 set memory . . . . . . . . . 38 Simulations . . . . . . . . . . . 43 sort . . . . . . . . . . . . . . . . 23 Spreadsheets . . . . . . . . . . . 48 SPSS . . . . . . . . . . . . . . . . . 48 st command family . . . . 33 StataQuest . . . . . . . . . . . 3, 8 StatTransfer . . . . . . . . . . . 48 stcox . . . . . . . . . . . . . . . 34 stptime . . . . . . . . . . 34, 35 Stratified analysis . . . . . . . 28 string function . . . . . . 40 String variables . . . . . . . . 39 sts graph . . . . . . . . . . . 34 sts list . . . . . . . . . . . . 34 sts test . . . . . . . . . . . . 34 stset . . . . . . . . . . . . . . . 33 stsplit . . . . . . . . . . . . . 35 stsum . . . . . . . . . . . . . . . 35 Study power . . . . . . . . . . . 45 substr function . . . . . . 40 summarize . . . . . . . . . . . 25 Survival analysis . . . . . . . 33

T T-test . . . . . . . . . . . . . . . . Tab-separated data . . . . . . tab1 . . . . . . . . . . . . . . . . tab2 . . . . . . . . . . . . . . . . tabi . . . . . . . . . . . . . . . . table . . . . . . . . . . . . . . . tabodds . . . . . . . . . . . . . tabulate . . . . . . . . . . . . tabx.ado (user program) . . time.ado (user program) . . ttest, ttesti . . . . . . .

57

30 17 26 27 27 29 28 26 46 46 30

V Value label . . . . . . . . . . . . 18 Variable label . . . . . . . . . . 18 Variable lists . . . . . . . . . . 12 Variable names . . . . . . . . 10 Variables . . . . . . . . . . . . . 10 Variables window . . . . . . . 5 Variance homogeneity . . . 30 Viewer window . . . . . . . . . 6

W Weighting observations . . 13 whelp . . . . . . . . . . . . . . . . 7 Wilcoxon test . . . . . . . . . . 30

X xi: prefix . . . . . . . . . . . . 31

View more...

Comments

Copyright © 2017 PDFSECRET Inc.