Hello Everyone,
I am trying to create a variable ,"grad_AA", that will determine respondents' graduation year using the variable D_AA, which records respondents' graduation dates in month. The value of this variable is between 235 and 456 months. The year of graduation is determine using a interval. For example when D_AA is between [235; 240] the correspondent graduation year is 1999.
I have tried this following loop but it is giving me errors.
Can anyone help me figure out what is wrong?
foreach i of D_AA 235/456{
gen grad_AA `i'=1999 if `i'>=235 & <=240
replace grad_AA `i' = 2000 if `x' >=241 & <=252
replace grad_AA`i'=2001 if `i' >=253 & G4<=264
replace grad_AA`i'=2002 if `i' >=264 & <=276
replace grad_AA`i' =2003 if `i' >=277 & <=288
replace grad_AA`i' =2004 if `i' >=289 & <=300
replace grad_AA`i'=2005 if `i' >=301 & <=312
replace grad_AA`i' =2006 if `i' >=313 & <=324
replace grad_AA`i'=2007 if `i' >=325 & <=336
replace grad_AA`i' =2008 if `i' >=337 & <=348
replace grad_AA`i' = 2009 if `i' >=349 & <=360
replace grad_AA`i' =2010 if `i' >=361 & <=372
replace grad_AA `i' =2011 if `i' >=372 & <=384
replace grad_AA`i' =2012 if `i' >=385 & <=396
replace grad_AA`i' =2013 if `i' >=397 & <=408
replace grad_AA`i' =2014 if `i' >=409 & <=420
replace grad_AA`i' =2015 if `i' >=421 & <=432
replace grad_AA `i' =2016 if `i' >=433 & <=444
replace grad_AA`i' =2017 if `i' >=445 & <=456)
replace grad_AA`i' =2018 if `i' >=457 & <=468)
replace Grad_AA `i'=. if `i'=.
}
Thank you so much for your help.
0 Response to Creating a variable with several condition using the foreach loop
Post a Comment