lab1sol#

video: tutorial lab1.do lab1.log

data: transplants.dta

zoom: M T W Th F

Please use this lab as an opportunity to review the course material and prepare yourself for the homework questions. An answer key will be provided on Friday April 7, 2023.

  1. Start Stata and open your do-file editor. lab1.do

  2. You will now write your first do-file following the guidelines on structure, indentation, and annotation given in chapter: pwd.

  3. We want to load transplants.dta. But before that, let’s check your working directory. See the bottom left side of the console (the main Stata window). You may also type pwd on the console. Is it where your transplants.dta is located? If not, use one of these two methods:

       a) Next time, launch Stata by double-clicking on transplants.dta.

       b) Type cd c:\your\actual\path (windows OS) on the console (NOT in your do file). In class and on my Mac OS my directory was /Users/d/desktop

  1. Now let’s get back to your do file. Load transplants.dta (double-click on the file or type use transplants.dta, clear

  2. How many observations does the dataset have?

count
di c(N)
di _N
  1. How many adults (age>18) does the dataset have?

count if age>18
  1. How many observations have missing bmi?

count if missing(bmi)
  1. Generate a new variable called agecat. The value of agecat is 1 for patients younger than 18, 2 for those from 18 to 65, and 3 for those older than 65.

//method 1
generate agecat=.
replace agecat=1 if age<18
replace agecat=2 if age>=18 & age<=65
replace agecat=3 if age>65 & !missing(age)

//method 2
gen byte agecat = 1+(age>=18)+(age>65) if !missing(age)
  1. What are the means of age and bmi?

sum age bmi
  1. Now preserve your dataset (type help preserve to learn more).

preserve
  1. Drop all patients who are younger than 18 or older than 65, or have missing value for age.

drop if age<18 | age>65 | missing(age)
  1. Again, what are the means of age and bmi? restore the dataset. Yet again, what are the means of age and bmi?

sum age bmi
restore
sum age bmi
  1. What happened? Leave a comment on your do-file explaining what you just did. Remember, your homework (and all other) .do file scripts should have 1-3 comments per block of code. Do not use your comments/annotation to define the Stata commands you’ve used. Thats what the help command is for. Instead, give yourself or a reader some context.

// ¯\_(ツ)_/¯
  1. Lab 1 is almost over. Let Stata say “that was easy!”

di "that was easy!"
//Black magic always *seems* easy... until you pay the price
  1. The last bit. Close your log file. Never forget to close log files! If you hadn’t created a log file, this is a reminder to do so. Preferably in the block of code that you may have designated using if 1 {

log close 
  1. You have all your commands so far in your do file, right? Run your entire do file and make sure your do file does exactly the same thing.