Help using Base SAS procedures

How to output mean next to each observation?

Accepted Solution Solved
Reply
Contributor
Posts: 21
Accepted Solution

How to output mean next to each observation?

Hi,

 

What is the best way to output the average X (Earnings/price) per year into the dataset with all my other variables. Here is the previous code I have tried but it is not including the mean EP per year next to each row (I need it to show per observation for my future calculation).

 

Thank you!

 

proc sort data=marketadj1;
by fyear X;
run;

proc means data=marketadj1;
var X;
by fyear;
run;

proc univariate data=marketadj1;
var x;
by fyear;
output mean=mean;
run;

proc print data=marketadj1 (obs=100);
run;

 

proc means data=marketadj1;
467 var X;
468 by fyear;
469 output=mean;
------
180
ERROR 180-322: Statement is not valid or it is used out of proper order.

470 run;

NOTE: The SAS System stopped processing this step because of errors.
NOTE: PROCEDURE MEANS used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds


471 proc univariate data=marketadj1;
472 var x;
473 by fyear;
474 out=meanstate mean n;
---
180
ERROR 180-322: Statement is not valid or it is used out of proper order.
475 run;

NOTE: The SAS System stopped processing this step because of errors.
NOTE: PROCEDURE UNIVARIATE used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds


476 proc univariate data=marketadj1;
477 var x;
478 by fyear;
479 output mean n;
-
73
ERROR 73-322: Expecting an =.
480 run;

NOTE: The SAS System stopped processing this step because of errors.
WARNING: The data set WORK.DATA1 may be incomplete. When this step was stopped there
were 0 observations and 0 variables.
NOTE: PROCEDURE UNIVARIATE used (Total process time):
real time 0.01 seconds
cpu time 0.00 seconds

 

481
482 proc univariate data=marketadj1;
483 var x;
484 by fyear;
485 output mean;
-
73
ERROR 73-322: Expecting an =.
486 run;

NOTE: The SAS System stopped processing this step because of errors.
WARNING: The data set WORK.DATA2 may be incomplete. When this step was stopped there
were 0 observations and 0 variables.
NOTE: PROCEDURE UNIVARIATE used (Total process time):
real time 0.03 seconds
cpu time 0.00 seconds


Accepted Solutions
Solution
‎02-25-2018 03:26 PM
Respected Advisor
Posts: 3,040

Re: How to output mean next to each observation?


dlazer1 wrote:

Hi,

 

What is the best way to output the average X (Earnings/price) per year into the dataset with all my other variables. Here is the previous code I have tried but it is not including the mean EP per year next to each row (I need it to show per observation for my future calculation).

 

Thank you!

 

proc sort data=marketadj1;
by fyear X;
run;

proc means data=marketadj1;
var X;
by fyear;
run;


If I am understanding the problem properly, you want the original data set, but with an additional column that contains the mean of X. Is that correct? Can you confirm?

 

Try this:

proc summary data=marketadj1 nway;
    class fyear;
    var x;
    output out=_stats_ mean=meanX;
run;
proc sort data=marketadj1;
    by fyear;
run;
data want;
    merge marketadj1 _stats_;
    by fyear;
run;
--
Paige Miller

View solution in original post


All Replies
Solution
‎02-25-2018 03:26 PM
Respected Advisor
Posts: 3,040

Re: How to output mean next to each observation?


dlazer1 wrote:

Hi,

 

What is the best way to output the average X (Earnings/price) per year into the dataset with all my other variables. Here is the previous code I have tried but it is not including the mean EP per year next to each row (I need it to show per observation for my future calculation).

 

Thank you!

 

proc sort data=marketadj1;
by fyear X;
run;

proc means data=marketadj1;
var X;
by fyear;
run;


If I am understanding the problem properly, you want the original data set, but with an additional column that contains the mean of X. Is that correct? Can you confirm?

 

Try this:

proc summary data=marketadj1 nway;
    class fyear;
    var x;
    output out=_stats_ mean=meanX;
run;
proc sort data=marketadj1;
    by fyear;
run;
data want;
    merge marketadj1 _stats_;
    by fyear;
run;
--
Paige Miller
Contributor
Posts: 21

Re: How to output mean next to each observation?

Posted in reply to PaigeMiller

Thank you!

☑ This topic is solved.

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

Discussion stats
  • 2 replies
  • 193 views
  • 0 likes
  • 2 in conversation