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

 

Good day everyone.

I am trying to run proc sql command and I am facing some difficulties

Background:

fatal represent numbers per case

countycode represent counties for each fatal.

I intend to generate a table that has unique countycodes and average fatal counts per county.

Below is my code

 

proc sql;
select fatals, countycode
	from county 
	group by countycode
	mean (fatals) as fatalcount;
quit; 

I keep getting syntax errors.

I will appreciate help with this.

Thank you 

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

Sir @PaigeMiller 

 

Even the corrected code 

proc sql;
select fatals, countycode, mean (fatals) as fatalcount
	from county 
	group by countycode;
quit;

will cause SQL processor the computed mean here fatalcount merge back(remerge) with the original. Of course we are not aware of what OP's exact WANT but methinks we can make a guess from "I intend to generate a table that has unique countycodes ".

Therefore excluding fatals from the SELECT should get the desired result.

 

proc sql;
select  countycode, mean (fatals) as fatalcount
	from county 
	group by countycode;
quit;

View solution in original post

6 REPLIES 6
PaigeMiller
Diamond | Level 26
proc sql;
select fatals, countycode, mean (fatals) as fatalcount
	from county 
	group by countycode;
quit;
--
Paige Miller
novinosrin
Tourmaline | Level 20

Sir @PaigeMiller 

 

Even the corrected code 

proc sql;
select fatals, countycode, mean (fatals) as fatalcount
	from county 
	group by countycode;
quit;

will cause SQL processor the computed mean here fatalcount merge back(remerge) with the original. Of course we are not aware of what OP's exact WANT but methinks we can make a guess from "I intend to generate a table that has unique countycodes ".

Therefore excluding fatals from the SELECT should get the desired result.

 

proc sql;
select  countycode, mean (fatals) as fatalcount
	from county 
	group by countycode;
quit;
oadeyemi
Obsidian | Level 7

Thank you.

This worked.

And I generated unique ID.

Thank you

oadeyemi
Obsidian | Level 7

Thank you.

I understand why I keep seeing the syntax error.

Although I did not get a unique ID though.

Thank you

Reeza
Super User

FYI - the majority of SQL is the same across all languages and most tutorials will work with SAS as well. 

 

Here's the w3 tutorials:

https://www.w3schools.com/sql/default.asp

 

And the SQL library in the documentation has many good examples:

https://support.sas.com/documentation/cdl//en/sqlproc/69822/HTML/default/viewer.htm#p016wt256y0c2on1...

 

See the side bar for other common usage cases.

 

oadeyemi
Obsidian | Level 7

Thank you for the link.

Very helpful.

Thanks for the information.

Catch up on SAS Innovate 2026

Dive into keynotes, announcements and breakthroughs on demand.

Explore Now →
Develop Code with SAS Studio

Get started using SAS Studio to write, run and debug your SAS programs.

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
  • 6 replies
  • 3779 views
  • 0 likes
  • 4 in conversation