12.07.2015 Views

Stata Tutorial - Data and Statistical Services - Princeton University

Stata Tutorial - Data and Statistical Services - Princeton University

Stata Tutorial - Data and Statistical Services - Princeton University

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Typing comm<strong>and</strong>s in the Comm<strong>and</strong> windowWe have already seen how to create a dummy variable (whoseoutcome is either 0 or 1) using –generate- <strong>and</strong> –replace-.Another easy way to create dummy variables is to use –tabulate- comm<strong>and</strong>. The –tabulate- comm<strong>and</strong>, when used witha generate option, produces dummy variables for each value.For example, suppose we want to create a dummy variable foreach of the outcomes of the categorical variable REP78.. tabulate rep78, gen(dumrep78)Suppose you want to group a continuous variable, PRICE, intofive equal ranges. First find out the minimum <strong>and</strong> maximumvalue that you want to use to group the PRICE by using –summarize-. Then,. generate ivprice = autocode(price,5,3291,15906)If you want to group PRICE into five groups of equalfrequencies, first sort PRICE, then issue the followingcomm<strong>and</strong>:. sort price. generate fqprice = group(5)Now, we have several more categorical variables to make a fourway table. Let’s create a table of repair records by HIREP byIVPRICE by FOREIGN. Here is how:. table rep78 hirep ivprice, by(foreign)You can label the variables so that you know what they are lateron. Let’s add a label to HIREP as an example.. label variable hirep “repair record is 3 or higher”. label define yesno 1 “yes” 0 “no”. lable values hirep yesnousing Menus<strong>Data</strong>=> Create orchange variables=>Other variablecreationcomm<strong>and</strong>s=> Createindicator variables<strong>Data</strong>=> Create orchange variables=>Create new variable,then enter autocodefunction in the box<strong>Data</strong>=> Sort=>Ascending sortStatistics=>Summaries, tables,<strong>and</strong> tests=>Tables=> Table ofsummarystatistics(table)<strong>Data</strong>=> Labels=>Label variable<strong>Data</strong>=> Labels=>Label values=>Define or modifyvalue labels<strong>Data</strong>=> Labels=>Label variable<strong>Data</strong>=> Labels=>Label values=>Assign value labelsto variableNotes <strong>and</strong> TipsScroll down the Variables Window to seewhat <strong>Stata</strong> created. Alternatively, viewthe list of variables by:. describeYou can also add notes to the variables.. note hirep: “temporary variable createdon October 1, 2006”When you describe data, (-describe-) youwill see an asterisk (*) by the variablelabel indicating that the variable hirep hasnotes.See the notes by typing. notesThe maximum number of variables youcan list in –table- is three.-label variable- adds a label to thevariable.-label define- defines values of a lable.The label name can be different from thevariable name, <strong>and</strong> can be used for othervariables.-label values- attach label to the variable.Review Questions:What is the comm<strong>and</strong> to1. create new variables?2. delete variables?3. regroup variables?4. group continuous variables?5. create dummy variables?Hints:. generate newvar =. drop varnames. recode oldvar (1/2=1) (3/4=2) (5=3),gen(newvar). generate varname = group(5). generate newvar =autocode(oldvar,5,min,max). generate newvar = 0. replace newvar = 1 if oldvar > 6165Page 21 of 28

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!