Hi,
Below is a data set, I need to find the identifier, its count and payout greater than 45
identiferpayoutregion
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 |
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
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
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).
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
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;
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!
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.
Ready to level-up your skills? Choose your own adventure.