Dear teacher Nick Cox , I have managed to do the exercise. The code generates the graphs I need. But, I am not sure whether it is the most efficient one (lack of confidence on the graph export mechanism; have very little experience with graph ). Kindly, check the Graph section code.
Code:
*----------------------------------------------------------------------------- * Data *----------------------------------------------------------------------------- clear input str32 country int year double(NGDPRPC NGDPDPC NGSD_NGDP PCPIPCH) "Cyprus" 2010 20490.637 31261.236 12.558 2.558 "Cyprus" 2011 20051.42 32692.698 14.706 3.481 "Cyprus" 2012 18936.417 29066.268 10.16 3.089 "Cyprus" 2013 17732.983 27825.87 8.246 .381 "Cyprus" 2014 17644.755 27267.428 8.022 -.268 "Estonia" 2010 11052.81 14672.328 23.075 2.741 "Estonia" 2011 11928.681 17470.842 26.442 5.08 "Estonia" 2012 12487.099 17431.54 27.143 4.219 "Estonia" 2013 12774.29 19078.213 27.426 3.247 "Estonia" 2014 13170.071 19969.128 26.937 .476 "Greece" 2010 20327.829 26972.873 5.664 4.704 "Greece" 2011 18464.601 25896.934 5.099 3.118 "Greece" 2012 17173.71 22171.911 8.972 1.035 "Greece" 2013 16742.04 21805.257 9.56 -.854 "Greece" 2014 16984.502 21726.89 10.28 -1.394 "Malta" 2010 15939.801 21150.423 18.954 2.041 "Malta" 2011 16113.851 22933.749 19.254 2.513 "Malta" 2012 16450.882 22069.52 19.718 3.227 "Malta" 2013 17005.342 24021.743 21.781 .979 "Malta" 2014 18097.074 26202.597 26.674 .771 end *label variables label variable NGDPRPC "Gross domestic product per capita, constant prices" label variable NGDPDPC "Gross domestic product per capita, current prices" label variable NGSD_NGDP "Gross national savings" label variable PCPIPCH "Inflation, average consumer prices" *----------------------------------------------------------------------------- * Generate Rank Variables *----------------------------------------------------------------------------- *generate rank variabels : largest value gets rank 1 sort year foreach v in NGDPRPC NGDPDPC NGSD_NGDP PCPIPCH { by year: egen rank_`v' = rank(-`v') } *----------------------------------------------------------------------------- * Graph *----------------------------------------------------------------------------- local names gen tosort = . quietly foreach var in NGDPRPC NGDPDPC NGSD PCPIPCH { separate rank_`var', by(country == "Cyprus") veryshortlabel gen(toplot) forval year= 2010/2014 { replace tosort = cond(year == `year', rank_`var', .) graph hbar (asis) toplot* if year==`year', nofill over(country, sort(tosort)) /// title("`: var label `var''" `year') b1title(Rank) legend(off) name(gr_`var'_`year', replace) saving(gr_`var'_`year', replace) local names `names' gr_`var'_`year' graph export gr_`var'_`year'.wmf, replace } drop toplot* } drop tosort graph close gr*
Comment