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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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