DATA Step, Macro, Functions and more

How can I get mean of one variable and median of another variable in a single proc means statement

Accepted Solution Solved
Reply
Contributor
Posts: 32
Accepted Solution

How can I get mean of one variable and median of another variable in a single proc means statement

Hi all,

 

Following is my code:

I need to calculate the mean of default_flag and median of P_1, and later create sgplot.

By using proc means, I am only able to get the mean values. Is there any way I can get mean of one and median of other in a single proc or other alternative.

 

proc means data=temp nway mean median;
     class snap_qtr qtr;
     var default_flag P_1;
     output out=data1 ;
run;
ods graphics on /  reset width=4in height=3in outputfmt=gif imagemap=on border=off;
title "SGPLOT Output";
ods layout start;
proc sgplot data=data1 ;

           series x=qtr y=default_flag/ legendlabel="Default Rate" markers lineattrs=(thickness=2);
           series x=qtr y=P_1/y2axis legendlabel="P rate" markers lineattrs=(thickness=2);
           xaxis display=(noline) interval=year grid  label="Time Period";
           yaxis display=(noline) grid  label="Default Rate" ;
           y2axis label="P rate";
run;
ods layout end;
title; 

Thanks in advance.


Accepted Solutions
Solution
‎12-21-2017 02:56 AM
Super User
Posts: 24,025

Re: How can I get mean of one variable and median of another variable in a single proc means stateme

Posted in reply to UshaLatha

Output out = want mean(variable1) = mean_var1 median(variable2) = median_var2;

 

You need to change the OUTPUT statement and you can refer to variables within parentheses to limit the output to the variables of interest. You may need to drop the VAR statement. 

View solution in original post


All Replies
Solution
‎12-21-2017 02:56 AM
Super User
Posts: 24,025

Re: How can I get mean of one variable and median of another variable in a single proc means stateme

Posted in reply to UshaLatha

Output out = want mean(variable1) = mean_var1 median(variable2) = median_var2;

 

You need to change the OUTPUT statement and you can refer to variables within parentheses to limit the output to the variables of interest. You may need to drop the VAR statement. 

PROC Star
Posts: 1,405

Re: How can I get mean of one variable and median of another variable in a single proc means stateme

Posted in reply to UshaLatha

Or use PROC SQL and do something like this

 

proc sql;
   create table data1 as
   select mean(default_flag) as mean_default_flag
         ,median(default_flag) as median_default_flag
         ,mean(P1) as mean_P1
         ,median(P1) as median_P1
   from temp
   group by snap_qtr, qtr;
quit;
Super User
Posts: 24,025

Re: How can I get mean of one variable and median of another variable in a single proc means stateme

In PROC SQL that works for SAS 9.4+
PROC Star
Posts: 1,405

Re: How can I get mean of one variable and median of another variable in a single proc means stateme

@Reeza, why only for 9.4+ ? Smiley Happy

Super User
Posts: 24,025

Re: How can I get mean of one variable and median of another variable in a single proc means stateme

@draycut The median function doesn't exist in PROC SQL for SAS 9.3 and lower versions.

Or at least not how you'd expect it to work, it calculates the median for the row, which for a single value is the single value. 

 

http://support.sas.com/kb/12/133.html

 

In 9.4 it calculates the median the same way PROC MEANS does, though I'm not sure what method it uses so you could potentially get different answers still if PCTLDEF was specified. 

http://support.sas.com/documentation/cdl/en/proc/65145/HTML/default/viewer.htm#n1qnc9bddfvhzqn105kqi...

 

 

 

 

PROC Star
Posts: 1,405

Re: How can I get mean of one variable and median of another variable in a single proc means stateme

Cool. Never knew that, thank you Smiley Happy

☑ This topic is solved.

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

Discussion stats
  • 6 replies
  • 181 views
  • 0 likes
  • 3 in conversation