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

Hello friends i am another proc sql query if someone can help please...

i have variables like,

date

store_no

item_no

sale_qty

onhand_qty

i want to see unique ItemNbr per store  +   sale_qty onhand_qty for that item_no on particular date

so may be my outcome would be like,

date               store_no         item_no        sale_qty       onhand_qty

--------------------------------------------------------------------------------------------------------

26mar14         123456             90055678         12                100

                                             90065656           0                10

                                             90078785         10                 50

                                             78528521           9                   1

                                             78585522           2                 35

I am using following logic but not working properly...

proc sql;

create table a as select date, item_no, count(item_no) as item_no_cnt, sum(sale_qty) as sale_qty_sum,

sum(onhand_qty) as onhand_qty_sum

from testdataset

where date='26mar14'd

group by item_no

having item_no_cnt>-1

order by item_no_cnt desc;

quit;

1 ACCEPTED SOLUTION

Accepted Solutions
amnist
Calcite | Level 5

You did not include date in the grouping clause. Please try this:

proc sql;

create table a as select date, item_no, count(*) as item_no_cnt, sum(sale_qty) as sale_qty_sum,

sum(onhand_qty) as onhand_qty_sum

from testdataset

where date='26mar14'd

group by date,item_no

having item_no_cnt>-1

order by item_no_cnt desc;

quit;

View solution in original post

6 REPLIES 6
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Hi,

Maybe break your logic down into smaller parts.

proc sql;

  create table ... as

  select  COALESCE(A.DATE,B.DATE) as DATE,

          COALESCE(A.STORE_NO,B.STORE_NO) as STORE_NO,

          COALESCE(A.ITEM_NO,B.ITEM_NO) as ITEM_NO,

          A.COUNT_OF_ITEM_NO,

          B.SUM_OF_SALE_QTY

  from    (select DATE,STORE_NO,ITEM_NO,COUNT(ITEM_NO) as COUNT_OF_ITEM_NO from ... group by DATE,STORE_NO) A

  full join (select DATE,STORE_NO,ITEM_NO,SUM(SALE_QTY) as SUM_OF_SALE_QTY from ... group by DATE,STORE_NO) B

  on      A.DATE=B.DATE

  and     A.STORE_NO=B.STORE_NO

  and     A.ITEM_NO=B.ITEM_NO;

quit;

So in the above, doing the sum in a subquery and the count in a subquery, then join them together.

murrak6
Calcite | Level 5

Try:

proc sql;

create table a as select date, item_no, count(*) as item_no_cnt, sum(sale_qty) as sale_qty_sum,

sum(onhand_qty) as onhand_qty_sum

from testdataset

where date='26mar14'd

group by item_no

having item_no_cnt>-1

order by item_no_cnt desc;

quit;

amnist
Calcite | Level 5

You did not include date in the grouping clause. Please try this:

proc sql;

create table a as select date, item_no, count(*) as item_no_cnt, sum(sale_qty) as sale_qty_sum,

sum(onhand_qty) as onhand_qty_sum

from testdataset

where date='26mar14'd

group by date,item_no

having item_no_cnt>-1

order by item_no_cnt desc;

quit;

jimksas
Calcite | Level 5

Hi Amnist - it worked fine - thanks lot!!!

amnist
Calcite | Level 5

Glad to help

jimksas
Calcite | Level 5

Amnist - i think we are missing "store_no" column here in this query- i just realize it...:-)

i want everything by "store_no" then "item_no" - exactly as i have shown in my first question thread...

thanks!!!

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

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
  • 6 replies
  • 2051 views
  • 0 likes
  • 4 in conversation