I got the result what i want at the following steps but i wonder to have better performance, are there any posibilities to get same result with less data steps or proc sql steps.
Data will work when you run the EG.
data default;
length default 8. Data 8. ;
infile datalines missover;
input Default Data ;
datalines;
1 0.0625
0 0.125
1 0.1875
0 0.25
1 0.3125
0 0.375
1 0.4375
0 0.5
1 0.5625
0 0.625
0 0.6875
1 0.75
1 0.8125
0 0.875
1 0.9375
;
proc sort data=work.default;
by descending Data;
run;
data work.cumulative;
set work.default;
NonDefault=1-Default;
by data notsorted;
if first.id then defaultRate=0;
defaultRate+default;
if first.id then NondefaultRate=0;
NondefaultRate+Nondefault;
run;
proc sql;
create table work.cumulative2 as
select default,data,NonDefault,defaultRate,NondefaultRate,sum(default) as TotalDefault,sum(NonDefault) as TotalNonDefault
from work.cumulative;
quit;
data work.cumulativeResult;
set work.cumulative2 END=lastN;
ResultD=defaultRate/TotalDefault;
ResultND=NondefaultRate/TotalNonDefault;
Retain Sonuc 0;
/*IF _n_=1 then Area=(ResultND/2)*ResultD;
ELSE*/Area=(ResultND+Lag(ResultND))/2*(ResultD-Lag(ResultD));
IF missing(Area)=1 then Area2=0;
Else Area2=Area;
Sonuc=Sonuc+Area2;
run;
proc sql;
create table cumulativeResultMax as
select max(Sonuc) as MaxSonuc
from cumulativeResult;
quit;
data PowerStat;
set work.cumulativeresultmax;
PowerStat=(MaxSonuc-0.5)*2;
ABSResult=ABS(PowerStat);
run;
You can replace your last 3 steps with this. Essentially, calculate the max within the data step and then generate your final result at the end of the data step.
data PowerStat;
set work.cumulative2 END=lastN;
ResultD=defaultRate/TotalDefault;
ResultND=NondefaultRate/TotalNonDefault;
Retain Sonuc 0 MaxSonuc 0;
/*IF _n_=1 then Area=(ResultND/2)*ResultD;
ELSE*/Area=(ResultND+Lag(ResultND))/2*(ResultD-Lag(ResultD));
IF missing(Area)=1 then Area2=0;
Else Area2=Area;
Sonuc=Sonuc+Area2;
if Sonuc>maxSOnuc then maxsonuc=sonuc;
if lastN then do;
PowerStat=(MaxSonuc-0.5)*2;
ABSResult=ABS(PowerStat);
output;
keep maxsonuc powerstat absresult;
end;
run;
You can replace your last 3 steps with this. Essentially, calculate the max within the data step and then generate your final result at the end of the data step.
data PowerStat;
set work.cumulative2 END=lastN;
ResultD=defaultRate/TotalDefault;
ResultND=NondefaultRate/TotalNonDefault;
Retain Sonuc 0 MaxSonuc 0;
/*IF _n_=1 then Area=(ResultND/2)*ResultD;
ELSE*/Area=(ResultND+Lag(ResultND))/2*(ResultD-Lag(ResultD));
IF missing(Area)=1 then Area2=0;
Else Area2=Area;
Sonuc=Sonuc+Area2;
if Sonuc>maxSOnuc then maxsonuc=sonuc;
if lastN then do;
PowerStat=(MaxSonuc-0.5)*2;
ABSResult=ABS(PowerStat);
output;
keep maxsonuc powerstat absresult;
end;
run;
Thanks a lot 🙂 Why i can't calculate Sum function at data step ? After this solution how can i take this values into new table with loop ?
These can probably help me but i can't handle it.
%let colCnt=<possibleValue>;
data out_data(keep=variable)
set work.new;
do i=1 to &colcnt.;
<????>
output;
%macro loop;
%let i=1;
%do %while(%scan(&VARNAME.,&i.,%STR( ))~=);
%procfreq(%scan(&VARNAME.,&i.,%STR( )));
%let i=&i.+1;
%end;
%mend;
%loop;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.