I have this table:
I would like to add two columns using SAS base :
- _idx_new that aggregates class having perc_tot_1<=1 with the next ones untill the new percentage is greater than 1
- the perc_tot_2 having the new percentages
Here the output table:
I tried with this SAS code to compute idx_new:
data test1;
set dataset;
by _idx;
perc_tot = _freq/&tot.;
perc_tot_1 = _freq/&tot.*100;
retain _idx_new 0;
if perc_tot_1 < 1 and lag1(perc_tot_1)>=1 then do;
_idx_new = _idx_new+1;
end;
if perc_tot_1 < 1 and lag1(perc_tot_1)<1 then do;
_idx_new = _idx_new;
a=1;
end;
run;
But it doesn't work properly. And I don't know how to compute the perc_tot_2.
Could you help me? Thanks!


I suspect you are working too hard. Since you didn't provide any data let's just make some up.
Now just process the data and when either the current percent or the previous percent is BIG then increment the new grouping variable. You also want to increment on the first observation, even if the percent is small (otherwise the group numbers will count from zero instead of one).
Result
If you then want to aggregate the little percentages you could just summarize by the new grouping variable.
Result: