Stata version 13 - UMasspeople.umass.edu/biep640w/pdf/stata v 13 entering... ·...

18
Stata Version 13 – Spring 2015 Getting Started: How to Enter and Output Data (mac) 1. Teaching\stata\stata version 13\stata version 13 – Spring 2015\stata v 13 entering and outputting data.docx 1/30/2015 Page 1 of 18 Stata version 13 Getting Started: How to Enter and Output Data January 2015 1. Things to Know Before You Begin …………….………………….. A. Variable Types and Storage ………………………………………. B. Tips ………………………………………………………………. 2. Entering Data …………………………………………………………. a. Import a Stata Data Set from the Internet ……………………..…….. b. Import Excel Data Set and save as a Stata Data Set ………………... c. Create a New Stata Data Set Using the Data Editor in Stata ……… 3. Outputting Data ……………………………………………………… a. Save Stata Data Set …………………………………………………. b. Export Stata Data Set to Excel Data Set …………………………… c. Export Stata Data Set to SAS Data Set …………………………….. 4. StatTransfer to Import and Export ……………………………….... 2 2 3 4 4 5 9 13 13 14 16 18

Transcript of Stata version 13 - UMasspeople.umass.edu/biep640w/pdf/stata v 13 entering... ·...

Page 1: Stata version 13 - UMasspeople.umass.edu/biep640w/pdf/stata v 13 entering... · Teaching\stata\stata version 13\stata version 13 – Spring 2015\stata v 13 entering and outputting

Stata Version 13 – Spring 2015 Getting Started: How to Enter and Output Data

(mac) 1. Teaching\stata\stata version 13\stata version 13 – Spring 2015\stata v 13 entering and outputting data.docx 1/30/2015 Page 1 of 18

Stata version 13

Getting Started: How to Enter and Output Data January 2015

1. Things to Know Before You Begin …………….………………….. A. Variable Types and Storage ………………………………………. B. Tips ………………………………………………………………. 2. Entering Data …………………………………………………………. a. Import a Stata Data Set from the Internet ……………………..…….. b. Import Excel Data Set and save as a Stata Data Set ………………... c. Create a New Stata Data Set Using the Data Editor in Stata ……… 3. Outputting Data ……………………………………………………… a. Save Stata Data Set …………………………………………………. b. Export Stata Data Set to Excel Data Set …………………………… c. Export Stata Data Set to SAS Data Set …………………………….. 4. StatTransfer to Import and Export ………………………………....

2 2 3

4 4 5 9

13 13 14 16

18

Page 2: Stata version 13 - UMasspeople.umass.edu/biep640w/pdf/stata v 13 entering... · Teaching\stata\stata version 13\stata version 13 – Spring 2015\stata v 13 entering and outputting

Stata Version 13 – Spring 2015 Getting Started: How to Enter and Output Data

(mac) 1. Teaching\stata\stata version 13\stata version 13 – Spring 2015\stata v 13 entering and outputting data.docx 1/30/2015 Page 2 of 18

1. Things to Know Before You Begin

A. Variable Types and Storage It actually matters: (1) if a variable is supposed to be numerical, you don’t want it to be inadvertently imported as a character variable; (2) A good practice is to use the storage you need only, and not waste space; and (3) date variables are their own special challenge. Review of Variable Types. In PubHlth 540, we distinguished a variety of types of measurement: discrete versus continuous. Within each of these broader headings of measurement type, we learned sub-types. These distinctions were important because, depending on the variable type, some analyses are possible while others are not. Depending on the variable type, some analyses are possible and others are not. Measurement Type Sub-Type

Discrete Nominal Ordinal

Continuous Interval Ratio

Stata functions a little differently. It distinguishes two types of data: numeric and string. This distinction is important for reasons having to do with data storage. Stata distinguishes variable types according to its storage (as opposed to analysis possibility) Data Type Storage Type Name Storage Used in bytes

Numeric: (Integer, ratio, date/time,

missing)

Byte Int Long Float* Double

1 2 4 4 8

String Str1 to str244 1 to 244 *The default storage of numeric data in Stata is as ‘float’; it provides 7 digits of accuracy after the decimal point. This is plenty for most (but not all) purposes

Page 3: Stata version 13 - UMasspeople.umass.edu/biep640w/pdf/stata v 13 entering... · Teaching\stata\stata version 13\stata version 13 – Spring 2015\stata v 13 entering and outputting

Stata Version 13 – Spring 2015 Getting Started: How to Enter and Output Data

(mac) 1. Teaching\stata\stata version 13\stata version 13 – Spring 2015\stata v 13 entering and outputting data.docx 1/30/2015 Page 3 of 18

B. Tips Tip #1. The default storage of numeric data in Stata is as ‘float’; it provides 7 digits of accuracy after the decimal point. This is plenty for most (but not all) purposes. Tip #2. Enclose string variable values in quotes. Tip #3. When creating data sets, especially large ones, choose a storage selection that uses only the space that you actually need (“parsimony”).

Float, Double - Use for continuous measurements Byte, Int, Long – Use for measurements that are integer valued only Str# - Use for measurements that are string valued (text) only

Tip #4. If you need to change the storage type for a variable, Stata has a command recast for this! For help with this command, type help recast in the command window. Be sure to scroll down to see examples..

Page 4: Stata version 13 - UMasspeople.umass.edu/biep640w/pdf/stata v 13 entering... · Teaching\stata\stata version 13\stata version 13 – Spring 2015\stata v 13 entering and outputting

Stata Version 13 – Spring 2015 Getting Started: How to Enter and Output Data

(mac) 1. Teaching\stata\stata version 13\stata version 13 – Spring 2015\stata v 13 entering and outputting data.docx 1/30/2015 Page 4 of 18

2. Entering Data A. Import a Stata Data Set from the Internet

To Import a Stata Data Set from the Internet use “http://fullurlpath”, clear Tip #1. Be sure to enclose the url in quotes. Tip #2. Be sure to use the option clear after the comma. Tip #3. After the import is complete, be sure to save the Stata data set on your computer (if you want to, that is). To do this:

__1. From the top menu bar FILE > SAVE AS __2. Be sure to include the extension .dta when naming your saved data set.

Examples - use “http://www.pauldickman.com/survival/ivf.dta”, clear use “http://people.umass.edu/biep640w/datasets/week02.dta”, clear use “http://people.umass.edu/biep640w/datasets/larvae.dta”, clear

Page 5: Stata version 13 - UMasspeople.umass.edu/biep640w/pdf/stata v 13 entering... · Teaching\stata\stata version 13\stata version 13 – Spring 2015\stata v 13 entering and outputting

Stata Version 13 – Spring 2015 Getting Started: How to Enter and Output Data

(mac) 1. Teaching\stata\stata version 13\stata version 13 – Spring 2015\stata v 13 entering and outputting data.docx 1/30/2015 Page 5 of 18

2. Entering Data B. Import Excel Data and Save as a Stata Data Set

Beware - Take care that the data types are correct, especially for date variables! There are multiple methods for importing an excel data set: (1) Copy and paste; (2) Importing the excel spreadsheet; and (3) using StatTransfer (The simplest but requires purchase of StatTransfer). METHOD 1 – Copy (from Excel) and Paste (into Stata)

Step 1 – Launch Excel. Open the file containing your excel data Example – Here we have 4 variables, of different types.

Step 2 – In Excel, use FORMAT > CELLS to format each column of data (numeric, text, custom, etc) Tip #1 For each variable, position cursor over the letter of the column (eg column A for formatting the variable ID) Tip #2 For DATE variables. For a date variable, you must format it as type = custom. Thus, here, you would issue the command FORMAT > CELLS > CUSTOM. At right then scroll down to choose m/d/yy Variable Format Cells as id numeric – then choose 0 places after the decimal point dob custom – then choose the type: m/d/yy gender text weight numeric - then choose 2 places after the decimal point

Page 6: Stata version 13 - UMasspeople.umass.edu/biep640w/pdf/stata v 13 entering... · Teaching\stata\stata version 13\stata version 13 – Spring 2015\stata v 13 entering and outputting

Stata Version 13 – Spring 2015 Getting Started: How to Enter and Output Data

(mac) 1. Teaching\stata\stata version 13\stata version 13 – Spring 2015\stata v 13 entering and outputting data.docx 1/30/2015 Page 6 of 18

Step 3 – In Excel, select the data to be copied, including row headings with variable names. Use EDIT > COPY to complete selection.

Step 4 – Launch Stata. From tool bar, click on the icon DATA EDITOR

Step 5 – Position cursor in cell Var1[1]. From the menu bar, use EDIT > PASTE SPECIAL At right, click on the box “Treat first row as variable names”

Page 7: Stata version 13 - UMasspeople.umass.edu/biep640w/pdf/stata v 13 entering... · Teaching\stata\stata version 13\stata version 13 – Spring 2015\stata v 13 entering and outputting

Stata Version 13 – Spring 2015 Getting Started: How to Enter and Output Data

(mac) 1. Teaching\stata\stata version 13\stata version 13 – Spring 2015\stata v 13 entering and outputting data.docx 1/30/2015 Page 7 of 18

You should now see the following

Step 6 – Close the DATA EDITOR window Tip – Don’t worry; data is not lost. You will save it later

Step 7 – Convert the Excel date variable (that fails to import as a date variable) into a Stata date variable that is a bona fide date variable. Eg; - * For dates with year recorded in 2 digits, all in the 1900’s: * Use the function date(stringvariable, “MD19Y”) with option “MD19Y” in quotes. generate dob2 = date(dob, “MD19Y”) format dob2 %tdNN/DD/CCYY drop dob rename dob2 dob describe

Page 8: Stata version 13 - UMasspeople.umass.edu/biep640w/pdf/stata v 13 entering... · Teaching\stata\stata version 13\stata version 13 – Spring 2015\stata v 13 entering and outputting

Stata Version 13 – Spring 2015 Getting Started: How to Enter and Output Data

(mac) 1. Teaching\stata\stata version 13\stata version 13 – Spring 2015\stata v 13 entering and outputting data.docx 1/30/2015 Page 8 of 18

Step 8 – From the menu bar, save your Stata data set using FILE > SAVE AS …

Step 9 – Issue the command describe to check that everything is alright.

Page 9: Stata version 13 - UMasspeople.umass.edu/biep640w/pdf/stata v 13 entering... · Teaching\stata\stata version 13\stata version 13 – Spring 2015\stata v 13 entering and outputting

Stata Version 13 – Spring 2015 Getting Started: How to Enter and Output Data

(mac) 1. Teaching\stata\stata version 13\stata version 13 – Spring 2015\stata v 13 entering and outputting data.docx 1/30/2015 Page 9 of 18

2. Entering Data C. Create a New Stata Data Set Using the Data Editor in Stata

Most of the time, you will import data into Stata for analysis from an Excel file or from a Stata data set available on the internent. Once in a while, however, you may want to create a new data set by launching Data Editor in Stata. Tip – The Data Editor icon is located on the top, horizontal, navigation bar

Example - We’ll create a Stata set using the same n=4 observations on 4 variables. The variables are of different types: string, date, and numeric. Tips - The variable id could be either string/character or numeric. Your choice. Date variables must be entered as string first, converted to date, and then formatted.

id

type: numeric

dob

type: date

gender

type: string/character

weight

type: numeric 1 3/26/1926 male 161.3 2 6/9/1956 female 120.1 3 4/1/1954 male 223.2 4 11/4/1951 female 124.0

Recommended preliminaries (1) In the command window, define your variable names (all lower case suggested) and initialize values to missing (period for numeric, “ “ for string). (2) Explicitly specify string variables as such. (3) Initially, your date variable a temporary name that makes clear that it is initially string.

. generate id=. . generate str8 dob_string="" . generate str8 gender="" . generate weight=.

Page 10: Stata version 13 - UMasspeople.umass.edu/biep640w/pdf/stata v 13 entering... · Teaching\stata\stata version 13\stata version 13 – Spring 2015\stata v 13 entering and outputting

Stata Version 13 – Spring 2015 Getting Started: How to Enter and Output Data

(mac) 1. Teaching\stata\stata version 13\stata version 13 – Spring 2015\stata v 13 entering and outputting data.docx 1/30/2015 Page 10 of 18

From the toolbar at top, click on data editor Stata will give you an empty spreadsheet with the variable names you specified.

Enter your data as you would if you were using Excel. Handy!! – numeric data will be shown in black, string data will be shown in red.

Page 11: Stata version 13 - UMasspeople.umass.edu/biep640w/pdf/stata v 13 entering... · Teaching\stata\stata version 13\stata version 13 – Spring 2015\stata v 13 entering and outputting

Stata Version 13 – Spring 2015 Getting Started: How to Enter and Output Data

(mac) 1. Teaching\stata\stata version 13\stata version 13 – Spring 2015\stata v 13 entering and outputting data.docx 1/30/2015 Page 11 of 18

From the main menu bar select Data > Variables Manager In the Variable Managers window, put your cursor over var1. Click to activate.

Click on your first variable (id) to select. The dialog boxes at right, under “Variable Properties” will activate. Type in a label if you like. Click on Apply at lower right Then click on the ► Repeat for the other variables. Close Variables Manager

All done entering data? Close Data Editor. You will be returned to the command window

Page 12: Stata version 13 - UMasspeople.umass.edu/biep640w/pdf/stata v 13 entering... · Teaching\stata\stata version 13\stata version 13 – Spring 2015\stata v 13 entering and outputting

Stata Version 13 – Spring 2015 Getting Started: How to Enter and Output Data

(mac) 1. Teaching\stata\stata version 13\stata version 13 – Spring 2015\stata v 13 entering and outputting data.docx 1/30/2015 Page 12 of 18

Here you convert a string variable to a date variable. In the command window: (1) Use the generate command together with the date function with the option “MDY” to convert the date data (entered as string) to a new date variable that Stata will recognizes as date data. (2) Use the format command to format your new date variable (otherwise any output will be incomprehensible!) (3) Drop the string variable version of your date variable. Tip! List a few observations to check your work.

. generate dob=date(dob_string, "MDY") . format dob %tdNN/DD/CCYY . drop dob_string . list +-----------------------------------+ | id gender weight dob | |-----------------------------------| 1. | 1 male 161 03/26/1926 | 2. | 2 female 111.8 06/09/1956 | 3. | 3 male 223.6 04/01/1954 | 4. | 4 female 116.2 11/04/1951 | +-----------------------------------+

Tip! How to create a numeric 0/1 indicator of female. Note the double equal signs in the logical operator.

. generate female=(gender=="female") . list +--------------------------------------------+ | id gender weight dob female | |--------------------------------------------| 1. | 1 male 161 03/26/1926 0 | 2. | 2 female 111.8 06/09/1956 1 | 3. | 3 male 223.6 04/01/1954 0 | 4. | 4 female 116.2 11/04/1951 1 | +--------------------------------------------+

Don’t forget to save your data! FILE > SAVE AS

. save "/ Users/cbigelow/Desktop/practice.dta" file /Users/cbigelow/Desktop/practice.dta saved

Page 13: Stata version 13 - UMasspeople.umass.edu/biep640w/pdf/stata v 13 entering... · Teaching\stata\stata version 13\stata version 13 – Spring 2015\stata v 13 entering and outputting

Stata Version 13 – Spring 2015 Getting Started: How to Enter and Output Data

(mac) 1. Teaching\stata\stata version 13\stata version 13 – Spring 2015\stata v 13 entering and outputting data.docx 1/30/2015 Page 13 of 18

3. Outputting Data A. Save a Stata Data Set

There are multiple methods for saving a Stata data set: (1) From the toolbar; (2) In the command window - Destination directory is the current directory; and (3) In the command window - Destination directory is not the current directory METHOD 1 – From the Tool Bar FILE > SAVE AS Stata will open the current directory for you. From there, browse and click. METHOD 2 – In the Command Window - Destination Directory is the Current Directory save filename, replace Tip 1 – Stata will supply the extension “.dta” for you. No need for you to type this. Tip 2 – The option replace tells Stata to overwrite any existing file with the same name in that directory METHOD 3 – In the Command Window – Destination directory is not the Current Directory save fullpath/filename, replace Tip 1 – Stata will supply the extension “.dta” for you. No need for you to type this. Tip 2 – The option replace tells Stata to overwrite any existing file with the same name in that directory Example . save z:\bigelow\teaching\691f\stata\exercise1.dta, replace Example . cd z:\bigelow\teaching\691f\stata . save exercise1.dta, replace

Page 14: Stata version 13 - UMasspeople.umass.edu/biep640w/pdf/stata v 13 entering... · Teaching\stata\stata version 13\stata version 13 – Spring 2015\stata v 13 entering and outputting

Stata Version 13 – Spring 2015 Getting Started: How to Enter and Output Data

(mac) 1. Teaching\stata\stata version 13\stata version 13 – Spring 2015\stata v 13 entering and outputting data.docx 1/30/2015 Page 14 of 18

3. Outputting Data B. Export Stata Data Set to Excel

Step 1. In Stata - Be sure you have saved your data as a stata data set if you want to keep a stata version. Step 2. Decide which variables you wish to export. This may be all of them. Step 3. From tool bar at top FILE > EXPORT > Data to Excel Spreadsheet (*.xls, *.xlsx) You should see the following “Export to Excel File” menu of selections

Page 15: Stata version 13 - UMasspeople.umass.edu/biep640w/pdf/stata v 13 entering... · Teaching\stata\stata version 13\stata version 13 – Spring 2015\stata v 13 entering and outputting

Stata Version 13 – Spring 2015 Getting Started: How to Enter and Output Data

(mac) 1. Teaching\stata\stata version 13\stata version 13 – Spring 2015\stata v 13 entering and outputting data.docx 1/30/2015 Page 15 of 18

Step 5. If you want ALL variables: Leave the “Variables” box empty If you want a selection of variables: List them in the box. Separate with blanks (not commas!) Click SAVE AS…

Step 6. In the SAVE AS screen, make your selections “Save As”, “Where”, and “File Format”. Click on SAVE Tip – You will then need to click on OK to complete the export.

Page 16: Stata version 13 - UMasspeople.umass.edu/biep640w/pdf/stata v 13 entering... · Teaching\stata\stata version 13\stata version 13 – Spring 2015\stata v 13 entering and outputting

Stata Version 13 – Spring 2015 Getting Started: How to Enter and Output Data

(mac) 1. Teaching\stata\stata version 13\stata version 13 – Spring 2015\stata v 13 entering and outputting data.docx 1/30/2015 Page 16 of 18

3. Outputting Data C. Export Stata Data Set to SAS Data Set

Step 1. In Stata - Be sure you have saved your data as a stata data set if you want to keep a stata version. Step 2. Decide which variables you wish to export. This may be all of them. Step 3. From tool bar at top FILE > EXPORT > SAS XPORT You should see the following “export sasxport” menu of selections If you want ALL variables: Leave the “Variables” box empty If you want a selection of variables: List them in the box. Separate with blanks (not commas!) Under Options: Select “Rename variables and value labels to meet SAS XPORT restrictions Under Value labels: Select “Save in “formats.xpf” Click Save AS …

Page 17: Stata version 13 - UMasspeople.umass.edu/biep640w/pdf/stata v 13 entering... · Teaching\stata\stata version 13\stata version 13 – Spring 2015\stata v 13 entering and outputting

Stata Version 13 – Spring 2015 Getting Started: How to Enter and Output Data

(mac) 1. Teaching\stata\stata version 13\stata version 13 – Spring 2015\stata v 13 entering and outputting data.docx 1/30/2015 Page 17 of 18

Step 5. Under SAVE AS Make your selections “Save As” (no extension needed) and “Where” Click SAVE

Your *.xpt file should now be in the directory you specified.

Page 18: Stata version 13 - UMasspeople.umass.edu/biep640w/pdf/stata v 13 entering... · Teaching\stata\stata version 13\stata version 13 – Spring 2015\stata v 13 entering and outputting

Stata Version 13 – Spring 2015 Getting Started: How to Enter and Output Data

(mac) 1. Teaching\stata\stata version 13\stata version 13 – Spring 2015\stata v 13 entering and outputting data.docx 1/30/2015 Page 18 of 18

4. StatTransfer to Import and Export

http://www.stattransfer.com/ordering/academic-new.html

Tip! If you anticipate having to manipulate data platforms on a regular basis, consider obtaining StatTransfer. It’s wonderful! As an Academic User, StatTransfer Can be Purchased at Discount