BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Ronein
Meteorite | Level 14

Hello

I want to calculate aggregation simple statistics to different groups.

Is there a shorter code that  can produce same results?

Maybe via proc format?

proc sql;
create table Want as 
select Type,
       count(*) as nr_rows  format=comma28.,
	   sum(MSRP) as SUM_MSRP  format=comma28.,
	   sum(Invoice) as SUM_Invoice format=comma28.
from  sashelp.cars
group by Type
union all
select 'SUV+Sports' as Type,
       count(*) as nr_rows  format=comma28.,
	   sum(MSRP) as SUM_MSRP  format=comma28.,
	   sum(Invoice) as SUM_Invoice format=comma28.
from  sashelp.cars
where Type  in ('SUV','Sports')
union all
select 'SUV+Hybrid' as Type,
       count(*) as nr_rows  format=comma28.,
	   sum(MSRP) as SUM_MSRP  format=comma28.,
	   sum(Invoice) as SUM_Invoice format=comma28.
from  sashelp.cars
where Type  in ('SUV','Hybrid')
union all
select 'Hybrid+SUV+Sedan+Sports' as Type,
       count(*) as nr_rows  format=comma28.,
	   sum(MSRP) as SUM_MSRP  format=comma28.,
	   sum(Invoice) as SUM_Invoice format=comma28.
from  sashelp.cars
where Type  in ('Hybrid','SUV','Sedan','Sports')
union all
select ' Truck+Wagon' as Type,
       count(*) as nr_rows  format=comma28.,
	   sum(MSRP) as SUM_MSRP  format=comma28.,
	   sum(Invoice) as SUM_Invoice format=comma28.
from  sashelp.cars
where Type  in ('Truck','Wagon')
union all
select 'ALL' as Type,
       count(*) as nr_rows  format=comma28.,
	   sum(MSRP) as SUM_MSRP  format=comma28.,
	   sum(Invoice) as SUM_Invoice format=comma28.
from  sashelp.cars
;
quit;


 
 
1 ACCEPTED SOLUTION

Accepted Solutions
Mazi
Pyrite | Level 9
proc format;
	value $type (multilabel notsorted)
		"SUV"    = "SUV"
		"Wagon"  = "Wagon"
		"Sports" = "Sports"
		"Sedan"  = "Sedan"
		"Truck"  = "Truck"
		"Hybrid" = "Hybrid"
		"SUV", "Truck", "Hybrid", "Wagon", "Sedan", "Sports" = "All"
		"SUV", "Sports" = "SUV+Sports"
		"SUV", "Hybrid" = "SUV+Hybrid"
		"Hybrid", "SUV", "Sedan", "Sports" = "Hybrid+SUV+Sedan+Sports"
		"Truck", "Wagon" = "Truck+Wagon";
run;


proc means data=sashelp.cars completetypes nway;
	class type / mlf preloadfmt order=formatted exclusive;
	format type $type.;
	var msrp invoice;
	output out = want
	 sum(msrp invoice)=/autoname;
run;

can you try this ?

View solution in original post

2 REPLIES 2
PaigeMiller
Diamond | Level 26

PROC SUMMARY/PROC MEANS with multi-label formats.

 

Example: https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.4/proc/n13dhme6o4ut3en1u8e58r16b3zf.htm#n13dhme...

 

IMHO, PROC SUMMARY/PROC MEANS should always be the choice for computing a lot of statistics. IMHO, SQL should be the last choice for computing a lot of statistics.

--
Paige Miller
Mazi
Pyrite | Level 9
proc format;
	value $type (multilabel notsorted)
		"SUV"    = "SUV"
		"Wagon"  = "Wagon"
		"Sports" = "Sports"
		"Sedan"  = "Sedan"
		"Truck"  = "Truck"
		"Hybrid" = "Hybrid"
		"SUV", "Truck", "Hybrid", "Wagon", "Sedan", "Sports" = "All"
		"SUV", "Sports" = "SUV+Sports"
		"SUV", "Hybrid" = "SUV+Hybrid"
		"Hybrid", "SUV", "Sedan", "Sports" = "Hybrid+SUV+Sedan+Sports"
		"Truck", "Wagon" = "Truck+Wagon";
run;


proc means data=sashelp.cars completetypes nway;
	class type / mlf preloadfmt order=formatted exclusive;
	format type $type.;
	var msrp invoice;
	output out = want
	 sum(msrp invoice)=/autoname;
run;

can you try this ?

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
  • 2 replies
  • 381 views
  • 14 likes
  • 3 in conversation