BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
pchappus
Obsidian | Level 7

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;
1 ACCEPTED SOLUTION

Accepted Solutions
ShiroAmada
Lapis Lazuli | Level 10
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.

 

View solution in original post

4 REPLIES 4
Astounding
PROC Star

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.

Reeza
Super User
Put the word 'calculated' in front of any calculated variables when using it later.
novinosrin
Tourmaline | Level 20

You have a logical error. My i request sample data of what you have and want plz

ShiroAmada
Lapis Lazuli | Level 10
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: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 995 views
  • 0 likes
  • 5 in conversation