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

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

 

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

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

3 REPLIES 3
Reeza
Super User

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). 

 

 

 

 

 

 

PGStats
Opal | Level 21

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
Ksharp
Super User

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;

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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