Introduction to Stata 8 - (GRIPS
Introduction to Stata 8 - (GRIPS
Introduction to Stata 8 - (GRIPS
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
16. Do-file examples<br />
Here follow short examples of do-files doing typical things. Find more examples in Take<br />
good care of your data. All major work should be done with do-files rather than by entering<br />
single commands because:<br />
1. The do-file serves as documentation for what you did.<br />
2. If you discover an error, you can easily correct the do-file and re-run it.<br />
3. You are certain that commands are executed in the sequence intended.<br />
Example 1 generates the first <strong>Stata</strong> version of the data, and example 2 generates a modified<br />
version. I call both do-files vital in the sense that they document modifications <strong>to</strong> the data.<br />
Such do-files are part of the documentation and they should be s<strong>to</strong>red safely. Safe s<strong>to</strong>rage also<br />
means safe retrieval, and they should have names telling what they do. My principle is this:<br />
In example 1 gen.wine.do generates wine.dta. In example 2 gen.visit12a.do<br />
generates visit12a.dta. This is different from example 3 where no new data are<br />
generated, only output. This do-file is not vital in the same sense as example 1 and 2, and it<br />
should not have the gen. prefix (the Never Cry Wolf principle).<br />
As mentioned in section 3 I prefer <strong>to</strong> use NoteTab rather than the do-file edi<strong>to</strong>r for creating<br />
do-files. The last command in a do-file must be terminated by a carriage return; otherwise<br />
<strong>Stata</strong> cannot 'see' the command.<br />
Example 1. gen.wine.do generates <strong>Stata</strong> data set wine.dta from ASCII file<br />
// gen.wine.do creates wine.dta 13.5.2001<br />
infix id 1-3 type 4 price 5-10 rating 11 ///<br />
using c:\dokumenter\wines\wine.txt<br />
// Add variable labels<br />
label variable id "Identification number"<br />
lab var type "Type of wine"<br />
lab var price "Price per 75 cl bottle"<br />
lab var rating "Quality rating"<br />
// Add value labels<br />
label define type 1 "red" 2 "white" 3 "rosé" 4 "undetermined"<br />
label values type type<br />
lab def rating 1 "poor" 2 "acceptable" 3 "good" 4 "excellent"<br />
lab val rating rating<br />
// Add data set label<br />
label data "wine.dta created from wine.txt, 13.5.2001"<br />
save c:\dokumenter\wines\wine.dta<br />
Example 2. gen.visit12a.do generates visit12a.dta from visit12.dta<br />
// gen.visit12a.do generates visit12a.dta with new variables.<br />
use c:\dokumenter\proj1\visit12.dta, clear<br />
// Calculate hrqol: quality of life score.<br />
egen hrqol=rsum(q1-q10)<br />
label variable hrqol "Quality of life score"<br />
label data "Visit12a.dta created by gen.visit12a.do, 02.01.2001"<br />
save c:\dokumenter\proj1\visit12a.dta<br />
65