BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
turcay
Lapis Lazuli | Level 10

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;

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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;

View solution in original post

3 REPLIES 3
Reeza
Super User

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;
turcay
Lapis Lazuli | Level 10

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;

Reeza
Super User
You can calculate sum at a data step level but its more complex and requires a DOW loop. That being said, I didn't thoroughly go over your program. I don't understand your next question. And since it looks unrelated I'll suggest posting a new question. L

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 948 views
  • 0 likes
  • 2 in conversation