Hi all,
I'm not too experienced with coding or with STATA.
For an experiment I'm currently generating new variables, let's say 'diffcont1995' for which I want to know what the difference is in variable 'varcr' in fiscal year 1995 compared to the base year (1994) for each unique company (gvkeyind). I'm doing the same for 'diffcont1997', 'diffcont1999' and 'diffcont2001'.
The same goes for variables diffsoxXXXX but these have base year 2002, and variables 'diffdoddXXXX have base year 2010.
I have tried different solutions but I'm unsuccessful to get the results that I want. Does anybody know how I can best solve this?
I'm not too experienced with coding or with STATA.
For an experiment I'm currently generating new variables, let's say 'diffcont1995' for which I want to know what the difference is in variable 'varcr' in fiscal year 1995 compared to the base year (1994) for each unique company (gvkeyind). I'm doing the same for 'diffcont1997', 'diffcont1999' and 'diffcont2001'.
The same goes for variables diffsoxXXXX but these have base year 2002, and variables 'diffdoddXXXX have base year 2010.
I have tried different solutions but I'm unsuccessful to get the results that I want. Does anybody know how I can best solve this?
Code:
gen diffcont1995=. gen diffcont1997=. gen diffcont1999=. gen diffcont2001=. gen diffsox2003=. gen diffsox2005=. gen diffsox2007=. gen diffsox2009=. gen diffdodd2011=. gen diffdodd2013=. gen diffdodd2015=. sum gvkeyind scalar max2=r(max) local k=max2 set more off forvalues i=1(1)`k'{ replace diffcont1995=l1.varcr-varcr if fyear=1995 & gvkeyind==`i' replace diffcont1997=l3.varcr-varcr if fyear=1997 & gvkeyind==`i' replace diffcont1999=l5.varcr-varcr if fyear=1999 & gvkeyind==`i' replace diffcont2001=l7.varcr-varcr if fyear=2001 & gvkeyind==`i' replace diffsox2003=l1.varcr-varcr if fyear=2003 & gvkeyind==`i' replace diffsox2005=l3.varcr-varcr if fyear=2005 & gvkeyind==`i' replace diffsox2007=l5.varcr-varcr if fyear=2007 & gvkeyind==`i' replace diffsox2009=l7.varcr-varcr if fyear=2009 & gvkeyind==`i' replace diffdodd2011=l1.varcr-varcr if fyear=2011 & gvkeyind==`i' replace diffdodd2013=l3.varcr-varcr if fyear=2013 & gvkeyind==`i' replace diffdodd2015=l5.varcr-varcr if fyear=2015 & gvkeyind==`i' di `i' " / " `k' }
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input float gvkeyind int fyear float varcr 1 1994 0 1 1995 0 1 1996 -1 1 1997 -1 1 1998 -1 1 1999 0 1 2000 0 1 2001 0 1 2002 0 1 2003 -1 1 2004 -4 1 2005 -4 1 2006 -4 1 2007 -4 1 2008 -3 1 2009 -3 1 2010 -3 3 1994 0 3 1995 0 3 1996 -5 3 1997 -5 3 1998 -5 3 1999 -5 3 2000 -5 3 2001 0 3 2002 0 3 2003 0 10 1998 1 10 1999 1 10 2000 1 10 2001 1 10 2002 1 10 2003 1 10 2004 0 10 2005 -1 10 2006 -1 10 2007 0 11 2000 5 13 1998 -4 13 1999 -4 15 1994 -2 15 1995 -2 15 1996 -2 15 1997 -2 15 1998 -1 15 1999 -1 15 2000 -1 15 2001 -1 15 2002 -3 15 2003 -7 15 2004 -10 15 2005 -10 15 2006 -10 15 2007 -6 15 2008 -6 15 2009 -7 15 2010 -7 16 1994 -1 16 1995 -1 16 1996 -1 16 1997 0 16 1998 5 16 1999 5 16 2000 6 16 2001 6 16 2002 1 16 2003 -3 16 2004 -6 16 2005 -7 16 2006 -6 18 1994 -4 18 1995 -5 18 1996 -5 24 1994 -1 24 1995 -1 24 1996 -1 24 1997 0 24 1998 0 24 1999 0 24 2000 0 24 2001 0 24 2002 0 24 2003 0 24 2004 0 24 2005 0 24 2006 -1 24 2007 -1 24 2008 -1 24 2009 -1 24 2010 -1 26 1994 13 26 1995 13 26 1996 13 26 1997 12 26 1998 12 26 1999 12 26 2000 12 26 2001 5 26 2002 10 26 2003 10 end
Comment