Pyrite | Level 9

## Average and median

Hi I need to find the code for average and median for the following example. Ans has to by groups and in a new column or row. Thanks in advance. Also I just need mean and median. No std and min and max.

Permno rtrn

0012     0.4

0012     0.5

0012     0.9

0013     0.8

0013     1.5

0013     0.2

0014     1.7

0014     1.8

0014     0.6

I tried  proc means but it gives std and min-max which I don't want.

1 ACCEPTED SOLUTION

Accepted Solutions
Super User

## Re: Average and median

@abdulla wrote:
proc means data=pred1 mean median;
by permno;
var abrtrn;
output out=pred2;
run;

I am not getting any median also in my work file. I have to use noprint too. And please make sure I don't want (N, std, Min Max)--- these come with mean in my work file.

The Mean and Median on the Proc statement control output to the results window.

Since you are creating an output set you have to request the statistics for the VAR variables. If no statistic is requested then you get a default set of statistics. Try:

```proc means data=pred1 mean median;
by permno;
var abrtrn;
output out=pred2 mean= median= /autoname;
run;```

The autoname suffixes the variable name with the statistic to differentiate the variables.

You can request different statistics for different variables by providing the variable name with the statistic such as:

mean(var1 var2)=  min (var2 var3)=.

There are quite a few options with the output option and you might want to read the documentation.

You may also want to investigate the different output you get using a CLASS statement instead of BY.

11 REPLIES 11
Diamond | Level 26

## Re: Average and median

Tell PROC MEANS not to give you those statistics you don't want, and give you only the statistics you do want.

``proc means data=have mean median;``
--
Paige Miller
Pyrite | Level 9

## Re: Average and median

I tried this solution by myself but it gives std, min and max value too. I don't want these results. I need only avg and median.
Can you give me an example? I am very new to SAS. So, I need help for that too.
Super User

## Re: Average and median

@abdulla wrote:
I tried this solution by myself but it gives std, min and max value too. I don't want these results. I need only avg and median.
Can you give me an example? I am very new to SAS. So, I need help for that too.

Show your code from the log.

```proc means data=sashelp.class mean median;
var height weight;
run;```
Pyrite | Level 9

## Re: Average and median

Again I know this solution. I don't want the following which comes with mean
(N, std, Min and Max value)
Now how will I tell the proc means not to give me the above value?
Pyrite | Level 9

## Re: Average and median

proc means data=pred1 mean median;
by permno;
var abrtrn;
output out=pred2;
run;

I am not getting any median also in my work file. I have to use noprint too. And please make sure I don't want (N, std, Min Max)--- these come with mean in my work file.

Diamond | Level 26

## Re: Average and median

@abdulla wrote:
I tried this solution by myself but it gives std, min and max value too. I don't want these results. I need only avg and median.
Can you give me an example? I am very new to SAS. So, I need help for that too.

Agreeing with @ballardw , I think you are mistaken, however until we see the code you used and the results you are getting, there's no way to resolve this.

--
Paige Miller
Pyrite | Level 9

## Re: Average and median

proc means data=pred1 mean median;
by permno;
var abrtrn;
output out=pred2;
run;

I am not getting any median also in my work file. I have to use noprint too. And please make sure I don't want (N, std, Min Max)--- these come with mean in my work file.
Super User

## Re: Average and median

@abdulla wrote:
proc means data=pred1 mean median;
by permno;
var abrtrn;
output out=pred2;
run;

I am not getting any median also in my work file. I have to use noprint too. And please make sure I don't want (N, std, Min Max)--- these come with mean in my work file.

The Mean and Median on the Proc statement control output to the results window.

Since you are creating an output set you have to request the statistics for the VAR variables. If no statistic is requested then you get a default set of statistics. Try:

```proc means data=pred1 mean median;
by permno;
var abrtrn;
output out=pred2 mean= median= /autoname;
run;```

The autoname suffixes the variable name with the statistic to differentiate the variables.

You can request different statistics for different variables by providing the variable name with the statistic such as:

mean(var1 var2)=  min (var2 var3)=.

There are quite a few options with the output option and you might want to read the documentation.

You may also want to investigate the different output you get using a CLASS statement instead of BY.

Pyrite | Level 9

## Re: Average and median

The code worked nicely. Thanks a lot.
What will be code if I want to use data statement instead of proc? Also, I have some missing values in my abrtrn column. How will I exclude these missing values?

If I use the above proc statement, where will I write "noprint"?

Diamond | Level 26

## Re: Average and median

@abdulla wrote:

What will be code if I want to use data statement instead of proc?

Don't do this in a data step. It's very simple in PROC MEANS.

Also, I have some missing values in my abrtrn column. How will I exclude these missing values?

PROC MEANS handles missing values properly. No need to exclude them.

If I use the above proc statement, where will I write "noprint"?

Some things are easily found by looking in the documentation for PROC MEANS.

https://documentation.sas.com/?docsetId=proc&docsetTarget=p0f0fjpjeuco4gn1ri963f683mi4.htm&docsetVer...

--
Paige Miller
Pyrite | Level 9

## Re: Average and median

Thanks Paige Miller
Discussion stats
• 11 replies
• 7456 views
• 0 likes
• 3 in conversation