I'm getting an error of Summary functions are restricted to the SELECT and HAVING ...
Is this because I can't group by a variable in the same statement that I create it in?
PROC SQL;
CREATE TABLE work.counts AS
SELECT
Report_Month,
CASE
WHEN P = 'ACC' THEN 'A'
WHEN P = 'DCL' AND S = 'ACC' THEN 'B'
WHEN P = 'DLC' AND S = 'DLC' AND SS = 'ACC' THEN 'C'
WHEN SS = 'DLC' THEN 'E'
END AS Risk,
COUNT(DISTINCT SSN) AS COUNT
FROM work.Master
GROUP BY
Report_Month,
Risk,
Count;
QUIT;
PROC SQL;
CREATE TABLE work.counts AS
SELECT
Report_Month,
CASE
WHEN P = 'ACC' THEN 'A'
WHEN P = 'DCL' AND S = 'ACC' THEN 'B'
WHEN P = 'DLC' AND S = 'DLC' AND SS = 'ACC' THEN 'C'
WHEN SS = 'DLC' THEN 'E'
END AS Risk,
COUNT(DISTINCT SSN) AS COUNT
FROM work.Master
GROUP BY
Report_Month,
calculated Risk;
QUIT;I removed COUNT from the group by. I added the keyword calculated before the variable RISK. SAS needs an identifying keyword when you use a derived column/field/variable when it is built and used within the same query.
Why not just remove COUNT from the GROUP BY clause? You're already getting one observation per REPORT_MONTH / RISK group ... COUNT doesn't need to be a grouping variable.
You have a logical error. My i request sample data of what you have and want plz
PROC SQL;
CREATE TABLE work.counts AS
SELECT
Report_Month,
CASE
WHEN P = 'ACC' THEN 'A'
WHEN P = 'DCL' AND S = 'ACC' THEN 'B'
WHEN P = 'DLC' AND S = 'DLC' AND SS = 'ACC' THEN 'C'
WHEN SS = 'DLC' THEN 'E'
END AS Risk,
COUNT(DISTINCT SSN) AS COUNT
FROM work.Master
GROUP BY
Report_Month,
calculated Risk;
QUIT;I removed COUNT from the group by. I added the keyword calculated before the variable RISK. SAS needs an identifying keyword when you use a derived column/field/variable when it is built and used within the same query.
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.