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.
@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.
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;
@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;
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.
@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.
@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.
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"?
@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.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.