UCLA Academic Technology Services HomeServicesClassesContactJobs
Search

Stata Class Notes
Let's Get Organized


1.0 Stata commands in this unit

. order
rename
label data
label variable
label define
label values
replace
recode
note:
notes
save, replace

2.0 Demonstration and Explanation

Let's begin by using a new data set, schdat.dta, it looks like this:

id  a1  t1  gender  a2  t2 tgender
 1  95  88     0    94  95    1
 2  63  86     1    61  94    1
 3  87  80     0    81  84    1
 4  79  70     0    79  87    0
 5  68  78     1    63  69    0
 6  64  87     1    82  96    0
 7  86  75     0    69  76    0
 8  81  94     1    93  92    1
 9  89  79     0    90  78    1
10  78  68     1    80  80    1

. use schdat, clear
describe

2.1 ordering & renaming

. order id gender tgender a1 a2 t1 t2
rename a1 assign1
rename a2 assign2
rename t1 midterm
rename t2 final
rename gender female
rename tgender tfemale

2.2 Some labels

. label data "Fall 1999 Stat 100 Scores"
label variable female "student gender"
label variable tfemale "teacher gender"
generate totavg = (assign1 + assign2 + midterm + final) / 4
label variable totavg "total score, divided by 4"
describe

2.3 variable labels and recode

Let's make labels showing that female and tfemale are coded 1=female and 0=male.

label define sex 1 "female" 0 "male"
label values female sex
label values tfemale sex
describe
tab1 female tfemale
tab1 female tfemale, nolabel

The label define command creates a definition for the values 0 and 1 called sex. The label values command connects the values defined for sex with the values in female and tfemale.

. generate grade = totavg
recode grade 0/60=0 60/70=1 70/80=2 80/90=3 90/100=4
label define abcdf  0 "F" 1 "D" 2 "C" 3 "B" 4 "A"
label values grade abcdf
list grade totavg

The  generate and recode commands make a new variable grade going from 1 to 5. Using label define and label values the values of grade are labeled A - F.

2.4 Make a note of this

. note: gender is self-report
note: the final was a take-home exam
notes
save schdat2
use schdat2, clear

3.0 Can you answer these questions?

  1. Do you think that the variables midterm and final need variable labels?
  2. Type summarize. What do the means of female and tfemale mean?
  3. What would happen if you did not save your data after recoding and adding labels and notes? Is there any other way to keep track of everything you have done so far?

4.0 For More Information

5.0 Web Notes

The Stata Class Notes are available on the World Wide Web by visiting ...
      http://www.ats.ucla.edu/stat/stata/notes/

The dataset schdat.dta can be loaded directly into Stata, over the Internet, using the following command:
use http://www.ats.ucla.edu/stat/stata/notes/schdat


How to cite this page

Report an error on this page

UCLA Researchers are invited to our Statistical Consulting Services
We recommend others to our list of Other Resources for Statistical Computing Help
These pages are Copyrighted (c) by UCLA Academic Technology Services


The content of this web site should not be construed as an endorsement of any particular web site, book, or software product by the University of California.