DATA Step, Macro, Functions and more

Fiscal Year Averages

Accepted Solution Solved
Reply
Contributor
Posts: 50
Accepted Solution

Fiscal Year Averages

Hi everyone,

 

I have a dataset that looks like this:

Fyear          Returns

1990             1

1990             3

1990             2

1991             1

1991             5 

1991             4

1992             4

1992              3

1992              2

I would want to create a new variable called, AVE, that will be equal to the average return for each fiscal year?

 May I know the best way to go about this?

 

Thanks


Accepted Solutions
Solution
‎09-19-2017 01:58 AM
Frequent Contributor
Posts: 113

Re: Fiscal Year Averages

[ Edited ]

Try this....

 

proc summary data=HAVE nway missing;
  class fyear;
  var returns;
output out= WANTDS1 mean=;
run;

/* OR you can sql */ proc sql; create table WANTSql as select fyear, mean(returns) as ave from HAVE group by fyear; quit;


/* OR you can sort and a data step */
proc sort data=HAVE;
by FYEAR;
run;

data WANT_DS2;
set HAVE;
by FYEAR;

if first.FYEAR then do;
count=.; ave=.;
end;

count+1;
ave+returns;

if last.fyear then do;
ave=ave/count;
output;
end;
run;
 

Hope this helps.

View solution in original post


All Replies
Solution
‎09-19-2017 01:58 AM
Frequent Contributor
Posts: 113

Re: Fiscal Year Averages

[ Edited ]

Try this....

 

proc summary data=HAVE nway missing;
  class fyear;
  var returns;
output out= WANTDS1 mean=;
run;

/* OR you can sql */ proc sql; create table WANTSql as select fyear, mean(returns) as ave from HAVE group by fyear; quit;


/* OR you can sort and a data step */
proc sort data=HAVE;
by FYEAR;
run;

data WANT_DS2;
set HAVE;
by FYEAR;

if first.FYEAR then do;
count=.; ave=.;
end;

count+1;
ave+returns;

if last.fyear then do;
ave=ave/count;
output;
end;
run;
 

Hope this helps.

Contributor
Posts: 50

Re: Fiscal Year Averages

Posted in reply to ShiroAmada
Thanks; it worked
☑ This topic is solved.

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

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