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

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
ballardw
Super User

@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.

View solution in original post

11 REPLIES 11
PaigeMiller
Diamond | Level 26

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
abdulla
Pyrite | Level 9
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.
ballardw
Super User

@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;
abdulla
Pyrite | Level 9
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?
abdulla
Pyrite | Level 9

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.

PaigeMiller
Diamond | Level 26

@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
abdulla
Pyrite | Level 9
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.
ballardw
Super User

@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.

abdulla
Pyrite | Level 9

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"? 

PaigeMiller
Diamond | Level 26

@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
abdulla
Pyrite | Level 9
Thanks Paige Miller

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 11 replies
  • 6939 views
  • 0 likes
  • 3 in conversation