I have two variables like overs and balls and i need to find the total count of overs and total number of runs?
input data :
overs runs
1 2
2 4
3 0
4 1
5 1
6 5
1 2
2 4
3 0
4 1
5 1
6 5
i need output like
overs runs
2 26
please try
data want;
input overs runs ;
cards;
1 2
2 4
3 0
4 1
5 1
6 5
1 2
2 4
3 0
4 1
5 1
6 5
;
proc sort data=want;
by overs;
run;
data want2;
set want;
by overs;
retain cnt;
if first.overs then cnt=1;
else cnt+1;
run;
proc sql;
create table want3 as select count(distinct cnt) as overs, sum(runs) as runs from want2 ;
run;
Why is overs=2 in your output?
Per Over 6 balls, here we are consider only 2 overs. However expected output will be 2 overs.
Ok. I think this gives you what you want
data want(drop=_:);
do until (lr);
set have end=lr;
_runs+runs;
if overs=1 then _overs+1;
end;
runs=_runs;overs=_overs;
run;
I am unable to understand that why can't we use
" _overs=_overs+1 " in place of "_overs+1" . Could you Please explain the difference between the two if possible.
data want(drop=_:);
do until (lr);
set have end=lr;
_runs+runs;
if overs=1 then _overs+1;
end;
runs=_runs;overs=_overs;
run;
please try
data want;
input overs runs ;
cards;
1 2
2 4
3 0
4 1
5 1
6 5
1 2
2 4
3 0
4 1
5 1
6 5
;
proc sort data=want;
by overs;
run;
data want2;
set want;
by overs;
retain cnt;
if first.overs then cnt=1;
else cnt+1;
run;
proc sql;
create table want3 as select count(distinct cnt) as overs, sum(runs) as runs from want2 ;
run;
Alternatively in a data step without proc sql
data want;
input overs runs ;
cards;
1 2
2 4
3 0
4 1
5 1
6 5
1 2
2 4
3 0
4 1
5 1
6 5
;
proc sort data=want;
by overs;
run;
data want2(rename=(cnt=overs sum=runs));
set want end=eof;
by overs;
retain cnt;
if first.overs then cnt=1;
else cnt+1;
sum+runs;
if eof;
keep cnt sum;
run;
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.