BookmarkSubscribeRSS Feed
adrfinance
Obsidian | Level 7

I have a dataset that looks like:

 

Variable name

qy                        balance

2010 Q1              100

2010 Q1              100

2010 Q2              150

2010 Q2              150

2010 Q3              50

2010 Q3              50

2010 Q4              20

2010 Q4              20

2011 Q1              300

2011 Q2              300

 

and I would like to create a new variable sum_balance_per_quarter that has the sum of the balances per quarter i.e. desired output:

 

  

Variable name

qy                        balance             sum_balance_per_quarter 

2010 Q1              100                    200

2010 Q1              100                    200

2010 Q2              150                    300

2010 Q2              150                    300

2010 Q3              50                      100

2010 Q3              50                      100

2010 Q4              20                       40

2010 Q4              20                       40

2011 Q1              300                      600

2011 Q2              300                      600

 

 

 

How can I do that?

1 REPLY 1
CarlosSpranger
Obsidian | Level 7

Hello adrfinance,

 

data testInput;
input qy $ balance;
datalines;
2010Q1 100
2010Q1 100
2010Q2 150
2010Q2 150
2010Q3 50
2010Q3 50
2010Q4 20
2010Q4 20
2011Q1 300
2011Q2 300
run;

 

proc sql;
SELECT qy, balance,
SUM (balance) as sum_balance_per_quarter
from testInput
GROUP BY qy;
quit;

 

Or you can improveit with subquery to show the overall Total Balance:

 

proc sql;
select qy,balance,sum_balance_per_quarter,SUM(sum_balance_per_quarter) as Total from (SELECT qy, balance,
SUM (balance) as sum_balance_per_quarter
from testInput
GROUP BY qy) as Result;
quit;

 

Hope it helps

 

Regards

CSB

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 1 reply
  • 361 views
  • 0 likes
  • 2 in conversation