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

Hello friends,

I want to ask if it is possible to use proc sql to calculate summary statistics per  grouping values.

For example:

Team variable has 3 possible values: a,b,c

I want to group the values by using proc format :  values a and b will belong to "a,b'  and value c to  "c"

 

As you can see in the code output2 is not the desired result.

Data tbl;
input ID Team $  Y;
CARDS;
1 a 2
2 b 4
3 b 6
4 c 8
5 a 10
6 a 12
7 a 14
8 c 16
9 c 18
10 c 20
;
run;
proc format;
Value $Ffmt
'a','b'='a,b'
'c'='c';
run;

PROC SQL;
	create table ouput1 as
	select 	Team ,
            count(*)  as No_Customers 
	from tbl
	group by Team
;
QUIT;


PROC SQL;
	create table ouput2 as
	select 	put(Team,$Ffmt.) as  Team_Category,
            count(*)  as No_Customers 
	from tbl
	group by put(Team,$Ffmt.)
;
QUIT;

 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

Try this SQL instead:

PROC SQL;
	create table ouput2 as
	select 	put(Team,$Ffmt.) as  Team_Category,
            count(*)  as No_Customers 
	from tbl
	group by calculated Team_Category
;
QUIT;

The SAS SQL compiler does not realize on its own that the expression in the group by is the same that was used for the creation of Team_Category, so you need to rub its nose in it.

View solution in original post

3 REPLIES 3
Kurt_Bremser
Super User

Try this SQL instead:

PROC SQL;
	create table ouput2 as
	select 	put(Team,$Ffmt.) as  Team_Category,
            count(*)  as No_Customers 
	from tbl
	group by calculated Team_Category
;
QUIT;

The SAS SQL compiler does not realize on its own that the expression in the group by is the same that was used for the creation of Team_Category, so you need to rub its nose in it.

Ronein
Onyx | Level 15

Perfect!!

I just want to under the logic.

Why  the  code:   group by put(Team,$Ffmt.)  is not working??

and your code :group by calculated Team_Category   is working?

They should be equivalent

 

Kurt_Bremser
Super User

@Ronein wrote:

Perfect!!

I just want to under the logic.

Why  the  code:   group by put(Team,$Ffmt.)  is not working??

and your code :group by calculated Team_Category   is working?

They should be equivalent

 


I already answered that. It's a shortcoming of the SAS SQL compiler. When it evaluates the group by, the expression with the put is a different object(!) than the newly created variable, although the same expression was used to create it, but that fact seems to no longer be available to the SQL compiler logic.

 

I just added a new idea to the Software Ballot:

https://communities.sas.com/t5/SASware-Ballot-Ideas/Enhance-the-quot-group-by-quot-evaluation-logic-...

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

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!

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 3 replies
  • 1365 views
  • 1 like
  • 2 in conversation