Hello all,
I am trying to optimize a program and am struggling with loop structures. After research I am still unable to do what I wish to do nor understand how to. Below, you will find the script I am trying to optimize into a loop and below that what I came up with however without success since the structure is wrong. To summarize, I am trying to create variables in two separate datasets with slightly different variable names from variables with slightly different names. In this case it is for 2 datasets so it is not too demanding to do it "by hand" but I'd like to automate the process in case in the future I'd encounter the same task for larger lists of datasets and better understand loop structures.
I think I understand that by nesting the foreach loops I have the child loops inside the iterations of the "parent" loop. Thus, using arguments from the child list twice by argument of the parent loop. I don't understand really how I can circumvent that problem with the number of arguments I have for the function.
I thank you in advance for your help.
I am using Stata17 on Ubuntu 20.04.4 LTS. The data is individual recode and men's recode data from the Demographic Health Survey for Senegal 2010/2011.
Incorrect function:
Best regards,
I am trying to optimize a program and am struggling with loop structures. After research I am still unable to do what I wish to do nor understand how to. Below, you will find the script I am trying to optimize into a loop and below that what I came up with however without success since the structure is wrong. To summarize, I am trying to create variables in two separate datasets with slightly different variable names from variables with slightly different names. In this case it is for 2 datasets so it is not too demanding to do it "by hand" but I'd like to automate the process in case in the future I'd encounter the same task for larger lists of datasets and better understand loop structures.
I think I understand that by nesting the foreach loops I have the child loops inside the iterations of the "parent" loop. Thus, using arguments from the child list twice by argument of the parent loop. I don't understand really how I can circumvent that problem with the number of arguments I have for the function.
I thank you in advance for your help.
I am using Stata17 on Ubuntu 20.04.4 LTS. The data is individual recode and men's recode data from the Demographic Health Survey for Senegal 2010/2011.
Code:
cd "../Datasets"
use SNIR61FL.DTA, clear //mother dataset
capture drop dobm
capture drop dobSIFm
capture drop bmonthm
capture drop byearm
capture drop educm
tostring v009 v010, gen(bmonthm byearm)
gen dobm=bmonthm+"/"+byearm
generate dobSIFm = monthly(dobm, "MY")
format dobSIFm %tm
label var dobSIFm "monthly date of birth of mother (SIF)"
gen educm = v106
label var educm "Mother's educational level"
cd "../Outputs"
save mother.DTA, replace
cd "../Datasets"
use SNMR61FL.DTA,clear //husband dataset
capture drop dobh
capture drop dobSIFh
capture drop bmonthh
capture drop byearh
capture drop educh
tostring mv009 mv010, gen(bmonthh byearh)
gen dobh=bmonthh+"/"+byearh
generate dobSIFh = monthly(dobh, "MY")
format dobSIFh %tm
label var dobSIFh "monthly date of birth of husband (SIF)"
gen educh = mv106
label var educh "Husband's educational level"
cd "../Outputs"
save husband.DTA, replace
Code:
foreach x in SNIR61FL.DTA SNMR61FL.DTA {
foreach p in m h {
foreach v in v mv {
foreach n in mother husband {
cd "../Datasets"
use `x', clear
capture drop dob`p'
capture drop dobSIF`p'
capture drop bmonth`p'
capture drop byear`p'
tostring `v'009 `v'010, gen(bmonth`p' byear`p')
gen dob`p'=bmonth`p'+"/"+byear`p'
generate dobSIF`p' = monthly(dob`p', "MY")
format dobSIF`p' %tm
label var dobSIF`p' "monthly date of birth of `n' (SIF)"
gen educ`p' = `v'106
label var educ`p' "`n''s educational level"
cd "../Outputs"
save `n'.DTA, replace
}
}
}
}

Comment