$title Repeated computation of percentiles within a loop (rank05,SEQ=139) Set h / 0*100 /; Parameter y(h) 'Aggregate expenditure associated with household type h' n(h) 'Number of persons associated with household type h' ypc(h) 'Per-capita expenditure of household type h' rank(h) 'Rank of household in per-capita expenditure'; * Assign some random values: y(h) = uniform(0.2,1.2); n(h) = uniform(1,6); ypc(h) = y(h)/n(h); * Assign ranks to household based on per-capita expenditures: $libInclude rank ypc h rank * Now determine percentile ranking of the households taking into account * differences in numbers of members and household representation: Set r 'Temporary set used for ranking' / r0*r100 /; Parameter pcttmp(r) 'Temporary array for computing percentiles' pct(h) 'Percentile rankings for households'; Set r0(r) / r0 /; * First, create an array with households assigned loop((r0(r),h), pcttmp(r+(rank(h)-1)) = n(h);); loop(r, pcttmp(r) = pcttmp(r) + pcttmp(r-1);); pcttmp(r) = pcttmp(r)/sum(h, n(h)); loop((r0(r),h), pct(h) = pcttmp(r+(rank(h)-1));); Parameter ranking 'Ranking of households and expenditures'; loop((r0(r),h), ranking(r+(rank(h)-1),h,"n") = n(h); ranking(r+(rank(h)-1),h,"ypc") = ypc(h); ranking(r+(rank(h)-1),h,"pct") = pct(h); ); display ranking;