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

Hi everyone,

 

I have a really easy question but somehow I dont get the right result. I have a table like this:

date balanceflag
01.04.2020100
01.04.2020201
02.04.2020100
02.04.2020100
02.04.2020101

And I want to summarize it, so that my result table looks like this:

 totalflag_0flag_1
01.04.2020301020
02.04.2020302010

 

Could you help me with posting the correct SAS code,thank you for the help.

 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26
proc summary data=have nway;
    class date flag;
    var balance;
    output out=want sum=;
run;

This gives you the sums. If you absolutely have to have it with flag_0 and flag_1 columns (why? it's usually not a good idea to make data wider) then PROC TRANSPOSE gets you there.

--
Paige Miller

View solution in original post

6 REPLIES 6
PaigeMiller
Diamond | Level 26
proc summary data=have nway;
    class date flag;
    var balance;
    output out=want sum=;
run;

This gives you the sums. If you absolutely have to have it with flag_0 and flag_1 columns (why? it's usually not a good idea to make data wider) then PROC TRANSPOSE gets you there.

--
Paige Miller
aguilar_john
Obsidian | Level 7
Thanks a lot!!
Also the PROC TRANSPOSE did work out. That is just the requested format...
novinosrin
Tourmaline | Level 20

The stat genius @PaigeMiller  is right about design, however for your requirement I would prefer Proc SQL-


data have;
input date mmddyy10. 	balance	flag;
format date mmddyy10.;
cards;
01.04.2020	10	0
01.04.2020	20	1
02.04.2020	10	0
02.04.2020	10	0
02.04.2020	10	1
;

proc sql;
create table want as
select date, sum(balance) as total,sum((flag=0)*balance) as flag0,sum((flag=1)*balance) as flag1
from have
group by date;
quit;
PaigeMiller
Diamond | Level 26

@novinosrin wrote:

The stat genius @PaigeMiller  is right about design, however for your requirement I would prefer Proc SQL-


data have;
input date mmddyy10. 	balance	flag;
format date mmddyy10.;
cards;
01.04.2020	10	0
01.04.2020	20	1
02.04.2020	10	0
02.04.2020	10	0
02.04.2020	10	1
;

proc sql;
create table want as
select date, sum(balance) as total,sum((flag=0)*balance) as flag0,sum((flag=1)*balance) as flag1
from have
group by date;
quit;

Let me explain why I prefer PROC SUMMARY over PROC SQL.

 

If you have lots of values of FLAG (instead of just two), the PROC SQL solution is a lot more typing. The PROC SUMMARY/PROC TRANSPOSE solution requires no additional typing, it works for any number of values of FLAG.

--
Paige Miller
novinosrin
Tourmaline | Level 20

I fully agree. I did think of it, however since I noticed Flag variable is "binary" , I was pretty confident there to suggest the SQL approach. That being said, the two step solution is certainly the one that will scale for any number of ID value suffix.

aguilar_john
Obsidian | Level 7
Many Thanks! that is also a nice way to do it

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

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 1654 views
  • 0 likes
  • 3 in conversation