DATA Step, Macro, Functions and more

Group by in sas Data step

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 8
Accepted Solution

Group by in sas Data step

Hi,

 

Below is a data set, I need to find the identifier, its count and payout greater than 45

 

identiferpayoutregion

1101
2202
3101
4203
5101
6202
7103
8203
9102
10204
11104
12202

 

I want output like:

regioncount identifiersum payout

 

2              4                              70

3              3                              50

 

How to group by in data step with aggregate functions using first. and last.

 

Thanks and Regards,

 

Ankita

 


Accepted Solutions
Solution
‎09-23-2016 01:48 AM
Respected Advisor
Posts: 4,663

Re: Group by in sas Data step

Even simple things can be done in many different ways. My favourite when dealing with BY groups, with FIRST and LAST conditions available, is FIRST - All - LAST.

 

FIRST - All - LAST logic : 

If first of group then
    set initialize sum to 0
add to sum (running sum)
if last of group and sum is over 45
    output

PG

View solution in original post


All Replies
Super User
Posts: 17,963

Re: Group by in sas Data step

Can you please post what you've tried. You're correct in needing first/last and BY group processing but it sounds like a homework assignment and hopefully you've attempted it. 

 

Logic. 

 

If first of group then

    set sum to first value

if last of group

    increment running total

    check if total is over 45

    if over 45 output

if not first or last

    add to total (running total). 

 

 

 

 

 

 

Solution
‎09-23-2016 01:48 AM
Respected Advisor
Posts: 4,663

Re: Group by in sas Data step

Even simple things can be done in many different ways. My favourite when dealing with BY groups, with FIRST and LAST conditions available, is FIRST - All - LAST.

 

FIRST - All - LAST logic : 

If first of group then
    set initialize sum to 0
add to sum (running sum)
if last of group and sum is over 45
    output

PG
Super User
Posts: 9,691

Re: Group by in sas Data step


data have;
infile cards expandtabs truncover;
input obs a b ;
cards;
1	10	1
2	20	2
3	10	1
4	20	3
5	10	1
6	20	2
7	10	3
8	20	3
9	10	2
10	20	4
11	10	4
12	20	2
;
run;

proc sql;
create table want as 
 select b,count(*) as count,sum(a) as sum 
  from have 
   group by b 
    having calculated sum gt 45 ;
quit;
☑ This topic is solved.

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

Discussion stats
  • 3 replies
  • 293 views
  • 1 like
  • 4 in conversation