DATA Step, Macro, Functions and more

Calculate mean using proc sql and case when

Accepted Solution Solved
Reply
Regular Contributor
Posts: 199
Accepted Solution

Calculate mean using proc sql and case when

I would like to find two means of a field called close_diff.
close_diff has some missing values.
One mean (mean_ca_close_diff) is conditional on incident_with_ca=1 and action (character field) not equal to missing.
The other (mean_noca_close_diff) mean is conditional on incident_with_ca=1 and action (character field) equal to missing.
The two means calculated using the code below are the same, however, they shouldn't be.
Any idea how to fix this?

 

proc sql;
create table EE_1 as
select *,
case when (incident_with_ca=1 and action ne "")
then mean(close_diff) else . end as mean_ca_close_diff,
case when (incident_with_ca=1 and action = "")
then mean(close_diff) else . end as mean_noca_close_diff
from EE
group by INJURY_YEAR
order by EHS_ID
;
quit;

 

Thank you.


Accepted Solutions
Solution
‎09-18-2017 11:49 PM
Super Contributor
Posts: 271

Re: Calculate mean using proc sql and case when

proc sql;
	create table EE_1 as
		select *,
	
		mean(case 
				when incident_with_ca=1 and action ne ""
				then close_diff else . 
			 end) as mean_ca_close_diff,
	
		mean(case 
				when incident_with_ca=1 and action = ""
				then close_diff else . 
			end) as mean_noca_close_diff
	from EE
		group by INJURY_YEAR
			order by EHS_ID
	;
quit;

View solution in original post


All Replies
Super Contributor
Posts: 271

Re: Calculate mean using proc sql and case when

Case when syntax is not correct.
Regular Contributor
Posts: 199

Re: Calculate mean using proc sql and case when

Posted in reply to SAS_inquisitive

OK, how should I fix the syntax?

Super Contributor
Posts: 271

Re: Calculate mean using proc sql and case when

I will post when I get home.
Solution
‎09-18-2017 11:49 PM
Super Contributor
Posts: 271

Re: Calculate mean using proc sql and case when

proc sql;
	create table EE_1 as
		select *,
	
		mean(case 
				when incident_with_ca=1 and action ne ""
				then close_diff else . 
			 end) as mean_ca_close_diff,
	
		mean(case 
				when incident_with_ca=1 and action = ""
				then close_diff else . 
			end) as mean_noca_close_diff
	from EE
		group by INJURY_YEAR
			order by EHS_ID
	;
quit;
Regular Contributor
Posts: 199

Re: Calculate mean using proc sql and case when

Posted in reply to SAS_inquisitive

Thank you.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 147 views
  • 0 likes
  • 2 in conversation