Hi, I'm new to Stata and still figuring out loops.
I'm creating a loop to clean two datasets. First I'm trying to rename some variables, then recode bmi into two categories. I am then trying to extract a substring from patient id to create an id with just numbers. I'm then saving the dataset and doing some checking.
My issue is that when I check the cleaned dataset, while the recoded bmi. has been changed in the dataset and the variable renaming has changed, the new variable (pt_id) has been added to the dataset but it has no values.
Can anyone suggest what I'm doing wrong?
Thanks!
* Define datasets BP1 and BP2
local datasets "BP1.dta BP2.dta"
* Loop through each dataset
foreach dataset in `datasets' {
* Load the dataset
use `dataset', clear
* Rename variables
rename age_group agegp
rename bmi_group bmigp
rename employment_group empgp
rename ses_group sesgp
* Recode bmi level into two groups to match the dataset description
replace bmigp = "0" if bmigp == "underweight" | bmigp == "normal"
replace bmigp = "1" if bmigp == "overweight" | bmigp == "obese"
* Extract numeric part from patient_id
gen pt_id = substr(patient_id, 2, .)
* Verify the extraction worked
list id pt_id in 1/10
* Save the cleaned dataset
local cleaned_filename = "`dataset'_cleaned"
save `cleaned_filename', replace
display "Saved cleaned dataset: `cleaned_filename'.dta"
*Summarize and check dataset
summarize
* Run codebook on bmigp
display "Details for variable bmigp in `dataset'"
codebook bmigp
}
I'm creating a loop to clean two datasets. First I'm trying to rename some variables, then recode bmi into two categories. I am then trying to extract a substring from patient id to create an id with just numbers. I'm then saving the dataset and doing some checking.
My issue is that when I check the cleaned dataset, while the recoded bmi. has been changed in the dataset and the variable renaming has changed, the new variable (pt_id) has been added to the dataset but it has no values.
Can anyone suggest what I'm doing wrong?
Thanks!
* Define datasets BP1 and BP2
local datasets "BP1.dta BP2.dta"
* Loop through each dataset
foreach dataset in `datasets' {
* Load the dataset
use `dataset', clear
* Rename variables
rename age_group agegp
rename bmi_group bmigp
rename employment_group empgp
rename ses_group sesgp
* Recode bmi level into two groups to match the dataset description
replace bmigp = "0" if bmigp == "underweight" | bmigp == "normal"
replace bmigp = "1" if bmigp == "overweight" | bmigp == "obese"
* Extract numeric part from patient_id
gen pt_id = substr(patient_id, 2, .)
* Verify the extraction worked
list id pt_id in 1/10
* Save the cleaned dataset
local cleaned_filename = "`dataset'_cleaned"
save `cleaned_filename', replace
display "Saved cleaned dataset: `cleaned_filename'.dta"
*Summarize and check dataset
summarize
* Run codebook on bmigp
display "Details for variable bmigp in `dataset'"
codebook bmigp
}

Comment