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.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.