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

I have a dataset that looks like:

 

qy                balance

2010 Q1      50

2010 Q1      50

2010 Q2      100

2010 Q2      100

2010 Q2      100

2010 Q3       200

2010 Q3       200

2010 Q3       200

 

and I would like to create a new column which contains the total sum i.e. desired output:

 

qy                balance         total_balance

2010 Q1      50                  1000

2010 Q1      50                  1000

2010 Q2      100                1000

2010 Q2      100                1000

2010 Q2      100                1000

2010 Q3       200               1000

2010 Q3       200               1000

2010 Q3       200               1000

 

 

How can I do that?

1 ACCEPTED SOLUTION

Accepted Solutions
ed_sas_member
Meteorite | Level 14

Hi @adrfinance 

 

Same logic as previously. You just need to remove the 'group by qy' clause:

 

data have;
	input qy $ 1-8 balance;
	datalines;
2010 Q1      50
2010 Q1      50
2010 Q2      100
2010 Q2      100
2010 Q2      100
2010 Q3       200
2010 Q3       200
2010 Q3       200
;
run;

proc sql;
	create table want as
	select qy, balance, sum(balance) as sum_balance
	from have;
quit;

Best,

View solution in original post

4 REPLIES 4
ed_sas_member
Meteorite | Level 14

Hi @adrfinance 

 

Same logic as previously. You just need to remove the 'group by qy' clause:

 

data have;
	input qy $ 1-8 balance;
	datalines;
2010 Q1      50
2010 Q1      50
2010 Q2      100
2010 Q2      100
2010 Q2      100
2010 Q3       200
2010 Q3       200
2010 Q3       200
;
run;

proc sql;
	create table want as
	select qy, balance, sum(balance) as sum_balance
	from have;
quit;

Best,

PeterClemmensen
Tourmaline | Level 20

In the data step approach..

 

data want;
    do until (lr1);
        set have end=lr1;
        sum_balance + balance;
    end;
    do until (lr2);
        set have end=lr2;
        output;
    end;
run;
adrfinance
Obsidian | Level 7

you have a typo in your code with the "haveend" or not?

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 4 replies
  • 598 views
  • 1 like
  • 3 in conversation