/*Lab Session Week 2 Generating Data, Merging and Reshaping Datasets*/ ******************************************************************************** ******************************************************************************** clear //Clears all data from memory cd "/Users/Rae/Desktop/Lab3" //modify filepath to your directory /* The goal here is to create two datasets that we will then use to practice merging and reshaping. The first data set will include 15 patients for whom we have a patient ID, Systolic Blood Pressure (SBP) for three years, and sex. The second data set will include the same 15 individuals and will have a fourth year's observation of SBP for the cohort. */ ******************************************************************************** *Beginning generation of Dataset 1 ******************************************************************************** set obs 15 //tells Stata to generate a new empty dataset with 15 observations (rows) set seed 523000 /* when using a random number generator, setting the seed tells the random number generator where to start generating numbers in its sequence. This allows you to generate reproducible results as the random number generator will begin in the same place every time you run the .do file. */ gen ID=_n //generates new variable called "ID" which takes on the value of the row ***************** *generating SBP for years 1-3, a normally distributed random variable *with a normally distributed mean average change from year to year ***************** gen SBP1=rnormal(130,20) //generates "SBP1" a normally distributed random variable with mean=130, standard dev. = 20 /* other data generating commands that you might find of interest include runiform() rbeta() rbinomial() rchi2() rpoisson() */ gen SBP2=SBP1+rnormal(3,5) /*generates "SBP2" a variable that takes on the value of each individual's year 1 SBP plus a change in SBP of mean=3 and std = 5. Under these data generating rules, the mean change in SBP from year one to year two is 3 mmHg with a standard deviation in the change of 5 mmHg.*/ gen SBP3=SBP2+rnormal(3,5) /* using the same data generating rule as "SBP2", we generate "SBP3". This is meant to follow a cohort of individuals who one might imagine is becoming less physically active or fit and has a mean rise in SPB of 30 mmHg over a period of 10 years*/ ****************** *generating sex, a random 1/0 variable ****************** gen female = rbinomial(1, 0.5) //generating a random binomial that is a 0/1 variable with mean = 0.5 /*The mean allows you to control the balance of population, thus if you wanted 2/3 of the sample to be female you would set the mean to 0.67. */ save Lab3_Dataset1.dta, replace //save this dataset as dataset1 ******************************************************************************** *Beginning generation of Dataset 2 ******************************************************************************** gen SBP4=SBP3+rnormal(3,5) //generating "SBP4" according to the same data generating rules as prior years drop SBP1 //droping variable here in order to demonstrate how to merge later on drop SBP2 //droping variable here in order to demonstrate how to merge later on drop SBP3 //droping variable here in order to demonstrate how to merge later on save Lab3_Dataset2.dta, replace //save this dataset as dataset2