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

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.

1 ACCEPTED SOLUTION

Accepted Solutions
SAS_inquisitive
Lapis Lazuli | Level 10
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

5 REPLIES 5
SAS_inquisitive
Lapis Lazuli | Level 10
Case when syntax is not correct.
gzr2mz39
Quartz | Level 8

OK, how should I fix the syntax?

SAS_inquisitive
Lapis Lazuli | Level 10
I will post when I get home.
SAS_inquisitive
Lapis Lazuli | Level 10
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;

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
  • 5 replies
  • 6248 views
  • 0 likes
  • 2 in conversation