BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
rohithverma
Obsidian | Level 7

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

1 ACCEPTED SOLUTION

Accepted Solutions
Jagadishkatam
Amethyst | Level 16

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;
Thanks,
Jag

View solution in original post

6 REPLIES 6
PeterClemmensen
Tourmaline | Level 20

Why is overs=2 in your output?

rohithverma
Obsidian | Level 7

Per Over 6 balls, here we are consider only 2 overs. However expected output will be 2 overs.

PeterClemmensen
Tourmaline | Level 20

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;
rohithverma
Obsidian | Level 7

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;

Jagadishkatam
Amethyst | Level 16

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;
Thanks,
Jag
Jagadishkatam
Amethyst | Level 16

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;

 

 

Thanks,
Jag

SAS Innovate 2025: Call for Content

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!

Submit your idea!

Mastering the WHERE Clause in PROC SQL

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.

Discussion stats
  • 6 replies
  • 1144 views
  • 1 like
  • 3 in conversation