turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- Base SAS Programming
- /
- Group by in sas Data step

Topic Options

- RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

09-22-2016 02:38 PM

Hi,

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

**identifer****payout****region**

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:

**region****count identifier****sum 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

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Ankita

09-22-2016 09:38 PM

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

All Replies

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Ankita

09-22-2016 02:53 PM

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

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Ankita

09-22-2016 09:38 PM

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

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Ankita

09-22-2016 10:17 PM

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;