I have a continuous variable “spread1”. I want to rescale it within industry – “sic2” between 0 and 1. I tried the code below, however it generates missing values. Can you please help me adjust this code?
Code:
set more off gen spread_rank = . levelsof sic2, local(tempyear) foreach i in `tempyear' { g decile_temp=(spread1 - r(min)) / (r(max) - r(min)) if sic2==`i' replace spread _rank = decile_temp if missing(spread _rank) drop decile_temp }
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input str6 gvkey double fyear byte sic2 float spread1 "001004" 1991 50 . "001004" 1995 50 1.1299435 "001004" 1996 50 1.6129032 "001004" 1997 50 .7100592 "001004" 1998 50 3.174603 "001004" 1999 50 1.793722 "001004" 2000 50 .9992887 "001004" 2001 50 .1749738 "001004" 2002 50 1.1173227 "001004" 2009 50 .05082708 "001004" 2010 50 .03787248 "001004" 2012 50 .1994963 "001004" 2013 50 .08229856 "001004" 2014 50 .0676999 "001009" 1991 34 5.128205 "001009" 1992 34 3.5714285 "001009" 1993 34 5.940594 "001009" 1994 34 3.5714285 "001013" 2009 36 .11997875 "001013" 2010 36 .07902196 "001034" 1995 28 4.295943 "001034" 1996 28 2.5316455 "001034" 1997 28 2.2988505 "001034" 1998 28 1.591512 "001034" 1999 28 1.6260163 "001034" 2000 28 1.1396011 "001034" 2001 28 1.6247886 "001034" 2002 28 .3355702 "001034" 2003 28 .04954287 "001034" 2004 28 .05894624 "001034" 2005 28 .035236377 "001034" 2006 28 .08287837 "001034" 2007 28 .0990612 "001036" 1997 34 1.9704434 "001036" 1998 34 2.1374047 "001036" 1999 34 3.75 "001036" 2000 34 5.181347 "001054" 1992 73 2.2222223 "001054" 1993 73 3.550296 "001056" 1999 38 6.022187 "001056" 2000 38 .12586533 "001056" 2001 38 7.373274 "001056" 2002 38 .57636833 "001056" 2003 38 -.25906712 "001056" 2004 38 -.06980962 "001056" 2005 38 0 "001056" 2006 38 .08565506 "001072" 1998 36 2.761341 "001072" 1999 36 1.8166804 "001072" 2000 36 . "001072" 2001 36 2.437277 "001072" 2002 36 .22271162 "001072" 2003 36 .18187746 "001072" 2004 36 .08160122 "001072" 2014 36 .07010325 "001078" 2015 28 .0222655 "001081" 2006 26 .3913891 "001094" 2004 51 .5129675 "001094" 2005 51 .3992044 "001098" 1994 36 2.690583 "001111" 1995 73 1.8867924 "001111" 1996 73 2.247191 "001111" 1997 73 .58309036 "001111" 1998 73 1.0050251 "001111" 2000 73 .25740024 "001111" 2001 73 .10028643 "001111" 2002 73 .13841148 "001111" 2003 73 .06322653 "001111" 2004 73 .06782115 "001111" 2005 73 0 "001111" 2006 73 .05284137 "001111" 2007 73 .10971178 "001115" 1994 38 4.511278 "001115" 1995 38 2.1052632 "001115" 1996 38 1.8808777 "001115" 1997 38 .3350084 "001115" 1998 38 .260078 "001115" 1999 38 .3169572 "001128" 1991 73 4.0816326 "001128" 1992 73 9.929078 "001128" 1993 73 6.31579 "001137" 1992 36 4.5454545 "001137" 1993 36 1.1976048 "001137" 1994 36 1.3071896 "001161" 1991 36 . "001161" 1992 36 .6920415 "001161" 1993 36 .7017544 "001161" 1994 36 .50377834 "001161" 1995 36 1.5037594 "001161" 1996 36 1.9417475 "001161" 1997 36 2.1201413 "001161" 1998 36 5.639913 "001161" 1999 36 . "001161" 2000 36 .4535147 "001161" 2001 36 .9419128 "001161" 2002 36 .1549222 "001161" 2003 36 .20140807 "001161" 2004 36 .0908286 "001161" 2005 36 .06542511 "001161" 2006 36 .09837904 end
0 Response to Rescale a variable between 0 and 1 within industry
Post a Comment