- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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