BookmarkSubscribeRSS Feed
braam
Quartz | Level 8

Dear All,

 

I have two sets, one is at an aggregated-level while the other is at a disaggregated-level. I would like to merge summary stat (e.g., mean, sum) from the second set with the first set by using proc SQL. I did this as shown below. I think it could be shorter, but I don't know how to do so. Please keep in mind that the aggregated-level has another variable that is not included in the the other data set. So I would like to use proc SQL to get summary information from the disaggregated set.

 

To illustrate, I generated the two datasets.

- ID_set: Set at an aggregated-level.

- Data_set: Set at a disaggregated-level.

 

*Generating ID_Set;
data ID_set;
	set sashelp.cars;
	keep Make Type; 
	run;

proc sort data= ID_set 
	nodupkey; 
	by Make;
	run;
	*N= 38;

*Generating Data_Set;
data Data_set;
	set sashelp.cars;
	keep Make EngineSize;
	run;
	*N= 428;

 

Using the two datasets, I would like to calculate sum and average of EngineSize and attach them to the first data set. I did PROC SQL and PROC SORT to keep the unique observations at an aggregated-level. I feel like they can be done at once in PROC SQL (without PROC SORT), but I don't know how to do so. Any help would be appreciated! 

 

 

proc sql;
	create table Merged
	as select a.*, 
		sum(b.EngineSize) as Sum_Engine,
		Mean(b.EngineSize) as Mean_Engine
	from ID_set a left join Data_set b
	on a.Make= b.Make
	group by a.Make;
	quit;

proc sort data= Merged nodupkey;
	by Make;
	run;

 

 

2 REPLIES 2
Reeza
Super User

So you want summaries in your data at the make level but the data table at a make/engine size categories?

TYPE is just the first record? It doesn't matter which one you take? Do you need it the logic for what's first to be consistent at least? Your current code doesn't guarantee that. 

sotojcr
Obsidian | Level 7
proc sql;
	create table Merged as 
       select a.*,
                 b. Sum_Engine,
                 b.Mean_Engine
       from       ID_set a 
       left join  (
                      select Make,
                                 sum(EngineSize) as Sum_Engine,
		                 Mean(EngineSize) as Mean_Engine
                      from Data_set
                      group by a.Make
                    ) b on a.Make= b.Make
quit;

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
  • 404 views
  • 0 likes
  • 3 in conversation