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: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

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
  • 665 views
  • 1 like
  • 3 in conversation