Hey all,
I'm having a SAS programming metal block and could use some help.
I am trying to create a dataset(s) that mimics an excel report that has the following structure:
The issue I'm having with converting into SAS is when using those slicers. Due to pivot #1's live link to the raw data, when retailer is chosen it knows what amounts to sum up even though the variable/column doesn't exist in that pivot.
So I am having trouble summing up at the brand and category level, but still being able to filter-down on other variables.
What I've tried so far:
PROC SQL; CREATE TABLE PIVOT AS SELECT A.RETAILER, A.PRODUCT, A.CATEGORY, A.BRAND, A.DATE, SUM(A.ActualValues) as Actuals, SUM(A.Forecast) as Forecast, B.ABS_VALUE FROM WORK.DATA as A LEFT JOIN (SELECT RETAILER, PRODUCT, CATEGORY, BRAND, DATE, ABS(SUM(ActualValues) - SUM(FORECAST)) AS B.ABS_VALUE FROM WORK.DATA GROUP BY 1,2,3,4,5 ) as B ON A.RETAILER = B.RETAILER AND A.PRODUCT = B.PRODUCT AND A.CATEGORY = B.CATEGORY AND A.BRAND = B.BRAND AND A.DATE = B.DATE GROUP BY 1,2,3,4,5,8; QUIT;
^ This didn't work since we have brands with multiple categories, so the output was incorrect when selecting a brand filter with multiple categories. Worked fine for for all others though.
I also tried to just make a mapping table where I could recreate the pivot tables in theory and then just map over pivot #1 so it would contain variables like RETAILER and PRODUCT. This didn't work because there's no one-to-one matching. A retailer may have many brands. and a brand may have many retailers.
Any thoughts?
Sounds like you don't want PROC SQL, you want to use ODS EXCEL and PROC REPORT together.
But for us to you need to provide example data as SAS data step code (instructions and examples). A lot of people ignore this request and provide data in some other format. DO NOT PROVIDE DATA IN SOME OTHER FORMAT!
We would also need to see a mock-up of the desired output (screen captures are ok in this case).
Hi,
when calculating the ABS_VALUE you can not call it "B.ABS_VALUE"
ABS(SUM(ActualValues) - SUM(FORECAST)) AS B.ABS_VALUE
should be
ABS(SUM(ActualValues) - SUM(FORECAST)) AS ABS_VALUE
PROC SQL;
CREATE TABLE PIVOT AS
SELECT A.RETAILER,
A.PRODUCT,
A.CATEGORY,
A.BRAND,
A.DATE,
SUM(A.ActualValues) AS Actuals,
SUM(A.Forecast) AS Forecast,
B.ABS_VALUE
FROM WORK.DATA AS A
LEFT JOIN
(SELECT RETAILER,
PRODUCT,
CATEGORY,
BRAND, DATE, ABS(SUM(ActualValues) - SUM(FORECAST)) AS ABS_VALUE
FROM WORK.DATA
GROUP BY 1,
2,
3,
4,
5) AS B ON A.RETAILER = B.RETAILER
AND A.PRODUCT = B.PRODUCT
AND A.CATEGORY = B.CATEGORY
AND A.BRAND = B.BRAND
AND A.DATE = B.DATE
GROUP BY 1,
2,
3,
4,
5,
8
;
QUIT;
- Cheers -
Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.
If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website.
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.