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

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

1 ACCEPTED SOLUTION

Accepted Solutions
ShiroAmada
Lapis Lazuli | Level 10

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

2 REPLIES 2
ShiroAmada
Lapis Lazuli | Level 10

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.

Theo_Gh
Obsidian | Level 7
Thanks; it worked

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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