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

I have a table below, I am trying to summing up the amount group by month variable.

But I am not getting required result with my query.

Please can some one help me to get proper output.

CustomerMonth1TypeAmount
A1Jan-04JEWELLERY188544.6983
A1Jan-04PETRO277066.25
A1Jan-04CLOTHES131813.031
A1Jan-04FOOD148244.2563
A1Jan-04TRAIN TICKET114180.6856
A1Jan-04TRAIN TICKET432552.6174
A1Feb-04BUS TICKET349329.4567
A1May-04BUS TICKET373086.1377
A1Jan-05CAMERA132804.9577
A1Feb-05SANDALS306648.4663
A1Feb-05CAR452239.2284
A1Feb-05FOOD372511.4839
A1Feb-05CLOTHES201655.2137
A1Aug-05MOVIE TICKET200904.7457
A1Nov-05PETRO412062.8038
A1Apr-06AIR TICKET263346.9597
A1Apr-06TRAIN TICKET491531.3638
A1Oct-06RENTAL152924.9167

 

1 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
72
73 proc sql;
74 select customer,month1,round(sum(amount),.01) as amt from capstone.spend
75 where customer ='A1'
76 group by customer,month1;
77 quit;
NOTE: PROCEDURE SQL used (Total process time):
real time 0.18 seconds
cpu time 0.17 seconds
 
 
78
79 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
91

I am getting below output.

Costomer

Month

amt

A1

JAN2004

277066.3

A1

JAN2004

302725.4

A1

JAN2004

131813

A1

JAN2004

580796.9

A1

FEB2004

349329.5

A1

MAY2004

373086.1

A1

JAN2005

132805

A1

FEB2005

452239.2

A1

FEB2005

372511.5

A1

FEB2005

508303.7

A1

AUG2005

200904.8

A1

NOV2005

412062.8

A1

APR2006

491531.4

A1

APR2006

263347

A1

OCT2006

152924.9

 

But i want only 9 rows in output.

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User
Month is probably a data variable with a format applied. SQL doesn't aggregate at the formatted values, but PROC MEANS will. Can you use one of the summary procs instead?
Otherwise, you can also apply the format to your code.
Not sure if you need the calculated key word in the GROUP BY or not....

Something like the following works but I would also highly recommend PROC MEANS instead.

proc sql;
select customer, put(month1, monyy5.) as month, round(sum(amount),.01) as amt from capstone.spend
where customer ='A1'
group by customer, calculated month;
quit;

proc means data=capstone.spend SUM NWAY maxdec=1 STACKODS;
where customer='A1';
CLASS Customer Month;
var amt;
ods output summary = want;
run;

View solution in original post

1 REPLY 1
Reeza
Super User
Month is probably a data variable with a format applied. SQL doesn't aggregate at the formatted values, but PROC MEANS will. Can you use one of the summary procs instead?
Otherwise, you can also apply the format to your code.
Not sure if you need the calculated key word in the GROUP BY or not....

Something like the following works but I would also highly recommend PROC MEANS instead.

proc sql;
select customer, put(month1, monyy5.) as month, round(sum(amount),.01) as amt from capstone.spend
where customer ='A1'
group by customer, calculated month;
quit;

proc means data=capstone.spend SUM NWAY maxdec=1 STACKODS;
where customer='A1';
CLASS Customer Month;
var amt;
ods output summary = want;
run;

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 1 reply
  • 346 views
  • 1 like
  • 2 in conversation