Re: Teenage pregnancies by year 2015 to 2022 [message #28790 is a reply to message #28690] |
Thu, 07 March 2024 15:50 |
Melyn
Messages: 2 Registered: February 2024
|
Member |
|
|
My goal is to analyse teenage pregnancies by year (2017 to 2022). This is what I have managed to do on Stata. My reshape command, even though it produces results, does not give the desired transformation of the dataset. This is where I am stuck and need help. I imagine a transformed dataset having 3 variables: Year (2017-2022), Total teenage pregnancies and Age-groups.
use KEIR8BFL.DTA, clear
*TEENAGERS: age-groups 15-19 and 20-24
keep if v013==1 | v013==2
*YEARS FOR ANALYSIS: keep only years 2017 to 2022 for the variables relating to year of pregnancy outcome (p2_01 to p2_20)
foreach var of varlist p2_01-p2_20 {
replace `var' = 0 if `var' < 2017 | `var' > 2022
}
*assess which variables have missing observations (zero values)
foreach var of varlist p2_01-p2_20 {
tabulate `var', missing
}
*drop pregnancy outcome variables with missing observations
drop p2_10-p2_20
*TEENAGE PREGNANCY VARIABLES: v201 "Total children ever born", v213 "Currently pregnant", v245 "Pregnancy losses"
*keeping only variables required for analysis of 2017 to 2022 trend analysis of teenage pregnancies for the age groups 15-19 and 20-24
keep v201 v213 v245 v013 p2_01-p2_09
*Recode "v213" into "preg_status_numeric" by generating a new variable "preg_status_numeric" based on "v213" such that "no or unsure" takes the value "0" and "yes" takes "1"
recode v213 (0=0) (1=1), generate(preg_status_numeric)
drop v213
*Rename variable to original name for ease of referencing
rename preg_status_numeric v213
*Generate a variable that sums up teenage pregnancies for the 3 related variables
gen Total_TeenagePreg=v201+v213+v245
*TRANSFORMING THE DATA for ease of analysis
*Sort the dataset by the age-group variable
sort v013
*Creating a new identifier variable named "id"
gen id = _n
*reshape the variables p2_01 through p2_09 from wide to long format, creating a new variable named outcome_year
reshape long p2_, i(id) j(outcome_year)
|
|
|