DATA Step, Macro, Functions and more

Less Data Step & less proc sql posibilites to get same result

Accepted Solution Solved
Reply
Super Contributor
Posts: 395
Accepted Solution

Less Data Step & less proc sql posibilites to get same result

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;


Accepted Solutions
Solution
‎09-25-2015 06:23 AM
Super User
Posts: 19,769

Re: Less Data Step & less proc sql posibilites to get same result

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


All Replies
Solution
‎09-25-2015 06:23 AM
Super User
Posts: 19,769

Re: Less Data Step & less proc sql posibilites to get same result

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;
Super Contributor
Posts: 395

Re: Less Data Step & less proc sql posibilites to get same result

Thanks a lot Smiley Happy 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;

Super User
Posts: 19,769

Re: Less Data Step & less proc sql posibilites to get same result

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
🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

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