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

Hi Experts,

I have a table with columns like

Col1,  col2, col3 , col4 , col5 , amount

for each distinct record for the combination of  ( Col1,  col2, col3 , col4 , col5 )   , I would like to get the count and the total sum ?

Below code is not working.... Can somebody help?  Please...

proc sql;

     select distinct (Col1,  col2, col3 , col4 , col5)  ,    count(*) as number , sum(amount)   from tablename   group by  distinct (Col1,  col2, col3 , col4 , col5) ;

quit;

Thanks in advance..

1 ACCEPTED SOLUTION

Accepted Solutions
damanaulakh88
Obsidian | Level 7

Hi Vijay,

Please find below the code and its output:-

==========================================

data new;

input col1 $ col2 $ col3 $ col4 $ col5 $ amount;

datalines;

a b c d e 23

d e f g h 34

a b g h j 36

a b c d e 45

a b c d e 90

d e f g h 80

a s d f g 70

;

run;

proc sql;

create table summarize as select distinct col1,col2,col3,col4,col5 ,count(*) as Count , sum(amount) as Sum from new group by col1,col2,col3,col4,col5;

quit;

===========================================

Output:-

=====================================

                                    Obs    col1    col2    col3    col4    col5    Count    Sum

                                     1      a       b       c       d       e        3      158

                                     2      a       b       g       h       j        1       36

                                     3      a       s       d       f       g        1       70

                                     4      d       e       f       g       h        2      114

=====================================

I hope this solves your query.

/Daman

View solution in original post

4 REPLIES 4
esjackso
Quartz | Level 8

I usually have better luck adding a subquery and I am assuming that the dups are existing in the amount column otherwise if its just the other columns the group by, by itself, should return distinct combinations of col1 - col5:

Proc sql;

     select col1, col2, col3, col4 , col5, count(*) as number, sum(amount) as amount

     from (select distinct col1, col2, col3, col4, col5, amount from tablename)

     group by col1, col2, col3, col4, col5

     ;

quit;

An example of the data may be in order to help explain the issue you are seeing.

EJ

VijayKumar
Calcite | Level 5

Thanks EJ for your answer.  This reply got me to a solution ... !!   Thank you!

After running the query I realized that I dont want the Distinct keyword...because

it will remove the duplicates and this will remove the amount also...which is not wanted....like

Sr. Nocol1col2col3col4col5amount
1MNOPQ100
2MNOPQ100
3MNOPQ100
4MNOPQ105
5XYZMN200

In above sample dataset example ,   the inner query ->  select distinct col1, col2, col3, col4, col5, amount from tablename  

will select only 1 ,4  and 5 row ...    as   2 and 3rd are duplicates of 1st

But end result shoudl be like 

Sr. Nocol1col2col3col4col5numberamount
1MNOPQ4405
2XYZMN1200

So I think the inner query will give wrong results...  am I right?

So for my requirement, the following query should be sufficient:

Proc sql;

     select col1, col2, col3, col4 , col5, count(*) as number, sum(amount) as amount

     from tablename

     group by col1, col2, col3, col4, col5

     ;

quit;

please correct if I am wrong...

I think earlier I didnt frame my question correctly. sorry about that...  and thanks for your help!


damanaulakh88
Obsidian | Level 7

Hi Vijay,

Please find below the code and its output:-

==========================================

data new;

input col1 $ col2 $ col3 $ col4 $ col5 $ amount;

datalines;

a b c d e 23

d e f g h 34

a b g h j 36

a b c d e 45

a b c d e 90

d e f g h 80

a s d f g 70

;

run;

proc sql;

create table summarize as select distinct col1,col2,col3,col4,col5 ,count(*) as Count , sum(amount) as Sum from new group by col1,col2,col3,col4,col5;

quit;

===========================================

Output:-

=====================================

                                    Obs    col1    col2    col3    col4    col5    Count    Sum

                                     1      a       b       c       d       e        3      158

                                     2      a       b       g       h       j        1       36

                                     3      a       s       d       f       g        1       70

                                     4      d       e       f       g       h        2      114

=====================================

I hope this solves your query.

/Daman

VijayKumar
Calcite | Level 5

Yes Daman, you are spot on!

Thanks for your reply and answer!

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 1764 views
  • 0 likes
  • 3 in conversation