I am using panel data, particularly the Panel Study of Income Dynamics (PSID). Each variable has a very different name each year. I am trying to generate variable names that are consistent. For example, age1968, age1969....age2003. In other words, I am trying to duplicate the original variables with consistent naming.
However, I am trying to do this conditional on each observation's value on another variable. In other words, I want to duplicate the values for only a subset of the variables. For example, gen age1969 = V1008 if ER30022 == 1 & ER30021 == 1. However, as there are a lot of variables and years, I'd rather not run hundreds of lines of code to do this for each individual variable. I'd rather do this through some loop. Below is the loop I tried.
The first variable that should be created in age1969 which should equal V1008 conditional on variables ER30022 == 1 & ER30021 == 1. However, the error I get says that "
variable age1969 already defined"
What am I doing wrong here? Any guidance?
However, I am trying to do this conditional on each observation's value on another variable. In other words, I want to duplicate the values for only a subset of the variables. For example, gen age1969 = V1008 if ER30022 == 1 & ER30021 == 1. However, as there are a lot of variables and years, I'd rather not run hundreds of lines of code to do this for each individual variable. I'd rather do this through some loop. Below is the loop I tried.
Code:
*Years local tvars 1969 1970 1971 1972 1973 1974 1975 1976 1977 /// 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 /// 1991 1992 1993 1994 1995 1996 1997 1999 2001 2003 *AGE 1969-2003 local avars V1008 V1239 V1942 V2542 V3095 V3508 /// V3921 V4436 V5350 V5850 V6462 V7067 V7658 V8352 /// V8961 V10419 V11606 V13011 V14114 V15130 V16631 V18049 /// V19349 V20651 V22406 ER2007 ER5006 ER7006 ER10009 ER13010 ER17013 ER21017 *Trying to create the variables for each age-year pair forval i = 1/32 { local temp : word `i' of `tvars' local a : word `i' of `avars' gen age`temp' = `a' if ER30022 == 1 & ER30021 == 1 }
The first variable that should be created in age1969 which should equal V1008 conditional on variables ER30022 == 1 & ER30021 == 1. However, the error I get says that "
variable age1969 already defined"
What am I doing wrong here? Any guidance?
Comment