Hi, I'm having a headache computing new variables based on the observation above. I have variables P_1, bad and goods in a merged table, from I am calculating as follows: Cumbad is 1 for the first observations, then Cumbad + bad. Is the cumulative values of bad. Cumgood is the same as cumbad. CumPercentBad is Cumbad / total of bads, which do not know how to calculate it. I tried max(bads) not success at all. So, just put the whole number as such. CumPercentGood the same as above. Total accepted is cumulative of the total of bads and goods in descending order. If it the first observation then total accepted 5000, else, total accepted minus bad (from the above observation), minus good (from the above observations). Solving the problem from total accepted should fix the problem with the rest of the variables. This is the code I tried. I uploaded a txt if you want to give it a try. data matrix;
set merged;
* by P_1;
* retain CumBad CumGood;
retain _all_ ;
If _n_ = 1 then CumBad + Bad; else CumBad = CumBad + Bad; * works fine;
If _n_ = 1 then CumGood + Good; else CumGood = CumGood + Good; * works fine;
Cum_Perc_Bad = CumBad / 747; * Total of Bads, in fact, 747 is the max value of CumBads. works fine;
Cum_Perc_Good = CumGood / 4253; * Total of Gods; 4253 is the max value of CumGood. works fine;
Abs_diff = abs(Cum_Perc_Bad - Cum_Perc_Good); * works fine;
if _n_ = 1 then Total_Accepted + 5000 ; else Total_Accepted = Total_Accepted - Bad - Good; * Total accepted starts in 5000. esle Total_Accepted - bad[n-1] and good [n-1]. It fails, as it takes the current bad and good value observations instead of the above ;
Accepted_rate = Total_Accepted / 5000 ; * 5000 total number of goods and bads, in fact, the sum of bads and goods. Works fine;
If _n_ = 1 then Num_Bads_among_Acc + 747; else Num_Bads_among_Acc = Num_Bads_among_Acc - Bad; * 747 is the total os bads. It fails, as it takes bad[n] instead of bad [n-1];
Bad_Rate_among_Acc = Num_Bads_among_Acc / Total_Accepted;
If _n_ = 1 then Num_Goods_among_Acc + 4253; else Num_Goods_among_Acc = Num_Goods_among_Acc - Good ; * 4253 is the total of goods. It fails, as it takes good[n] instead of good [n-1];
Marg_BR = Good / Total_Accepted ; * It works ;
run;
proc print data = matrix; format Accepted_rate Bad_Rate_among_Acc percent8.2; run; What I am getting is: The desired output would be like: Thank you in advance.
... View more