I have a dataset that looks like this:
Year Quarter Market Sales
2017 1 ASL 120
2015 4 DSL 60
2017 1 ASL 70
2015 4 DSL 100
I would like to add a column to this summing Sales for each year, quarter, and market. So I need something that looks like this in the end:
Year Quarter Market Sales MarketSize
2017 1 ASL 120 190
2015 4 DSL 60 160
2017 1 ASL 70 190
2015 4 DSL 100 160
This is one example of a calculation I need to do. I read that the easiest way to do a sumif on SAS is using proc sql but that would create a new table for each calculation which I would need to merge with my original table. I have dozens of calculations similar to this and it seems inefficient to do so that way. Is there a simpler way to make these calculations? I am a beginner with SAS.
Post test data in the form of a datastep!
As such this is not tested:
proc sql;
create table want as
select a.*,
(select sum(b.sales) from have b where b.market=a.market group by market) as marketsize
from have a;
quit;
You can do this with a single PROC SQL step or PROC MEANS and a merge. Here's a fully worked example:
https://github.com/statgeek/SAS-Tutorials/blob/master/add_average_value_to_dataset.sas
Given your example, PROC SUMMARY would work to compute MarketSize, then you could merge the results back into the original table.
data have;
input Year Quarter Market $ Sales;
cards;
2017 1 ASL 120
2015 4 DSL 60
2017 1 ASL 70
2015 4 DSL 100
;
proc sql;
create table want as
select *,sum(sales) as MarketSize
from have
group by year,market;
quit;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.
Ready to level-up your skills? Choose your own adventure.