I need to create a binary indicator (weightmedian_binary) based on whether that variable (weight) is below or above its median value (weightmedian_priceq5) within each quintile of another variable (price_q5). Given how confusing this sounds, I have tried to illustrate with an example using the auto dataset. The issue is I need to generate multiple variables like this - beyond just weight - all based on whether they are below/above their median value within each quintile of price. I am trying to write a do file, but I am having difficulties since I do not know how to reference the median values from levelsof, have the loop correctly iterate quintile values, etc. Any help would be greatly appreciated. Thanks in advance!
Code:
sysuse auto egen price_q5 = xtile(price), n(5) egen weightmedian_priceq5 = median( weight ), by( price_q5) levelsof weightmedian_priceq5 gen weightmedian_binary = . replace weightmedian_binary = 0 if price_q5==1 & weight<2640 replace weightmedian_binary = 1 if price_q5==1 & weight>=2640 replace weightmedian_binary = 0 if price_q5==2 & weight<2650 replace weightmedian_binary = 1 if price_q5==2 & weight>=2650 replace weightmedian_binary = 0 if price_q5==3 & weight<2670 replace weightmedian_binary = 1 if price_q5==3 & weight>=2670 replace weightmedian_binary = 0 if price_q5==4 & weight<3280 replace weightmedian_binary = 1 if price_q5==4 & weight>=3280 replace weightmedian_binary = 0 if price_q5==5 & weight<3890 replace weightmedian_binary = 1 if price_q5==5 & weight>=3890
Comment