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

Hi Guys,

I am trying to obtain the median value using median() function in PROC SQL  as follows (SAS9.4):

/* select  clause with additional columns: Name Height*/
proc sql;
	select Name,Sex,Height,median(Height) as Med_Height,mean(Height) as Mean_Height
	from sashelp.class
	group by sex
	having sex="M";
quit;

The results are as follows:

Jinlong_SUFE_1-1714675504743.png

However,  when removing columns Name, Height in select clause like this:

/* select clause without additional column */
proc sql;
	select Sex, median(Height) as Med_Height, mean(Height) as Mean_Height
	from sashelp.class
	group by sex
	having sex="M";
quit;

The results are as follows:

Jinlong_SUFE_2-1714675677209.png

Comparing the two tables, the mean value of Height is still 63.91 while the median of Height changes from 64.3 to 64.15 (of course the 64.15 is the right value).

 

Where did the first median value 64.3 come from? How did this difference (64.3 v.s. 64.15) happen? After all the mean value remains the same. I feel confused.

Can anyone explain this? Thanks a lot guys. Appreciate it. 

1 ACCEPTED SOLUTION

Accepted Solutions
data_null__
Jade | Level 19

What SAS version.  Mine is 9.4 (TS1M8) and I do not see difference.

Update:  Problem Note 66896: Inconsistent results are generated with the PROC SQL Median summary function 

proc sql;
	select Name,Sex,Height,median(Height) as Med_Height,mean(Height) as Mean_Height
	from sashelp.class
	group by sex
	having sex="M";
quit;

proc means data=sashelp.class n median mean;
   class sex;
   var height;
   run;
   
proc sql;
	select Sex, median(Height) as Med_Height, mean(Height) as Mean_Height
	from sashelp.class
	group by sex
	having sex="M";
quit;

Capture.PNG

View solution in original post

2 REPLIES 2
data_null__
Jade | Level 19

What SAS version.  Mine is 9.4 (TS1M8) and I do not see difference.

Update:  Problem Note 66896: Inconsistent results are generated with the PROC SQL Median summary function 

proc sql;
	select Name,Sex,Height,median(Height) as Med_Height,mean(Height) as Mean_Height
	from sashelp.class
	group by sex
	having sex="M";
quit;

proc means data=sashelp.class n median mean;
   class sex;
   var height;
   run;
   
proc sql;
	select Sex, median(Height) as Med_Height, mean(Height) as Mean_Height
	from sashelp.class
	group by sex
	having sex="M";
quit;

Capture.PNG

Jinlong_SUFE
Calcite | Level 5
Thx a lot. I have fixed my SAS using hot fixes.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
What is ANOVA?

ANOVA, or Analysis Of Variance, is used to compare the averages or means of two or more populations to better understand how they differ. Watch this tutorial for more.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 327 views
  • 2 likes
  • 2 in conversation