BookmarkSubscribeRSS Feed
Danny2020
Calcite | Level 5

I am trying to get the mean value for returns, but different companies contain different number of years, how could i figure these out? many thanks

 

ID Year SizeReturn Average
12016sml-2.1 
12017sml0.25 
12018med0.26-0.26009
21999sml0.5 
22000med-0.8 
22001med0.7 
22002med1.90.575
3 REPLIES 3
acordes
Rhodochrosite | Level 12
Proc Means Data=have;
Class ID;
Var return;
Output out=want mean= / autoname;
Run;
yabwon
Onyx | Level 15

Hi,

 

Two things:

1) how you get-0.26009 out of -2.1, 0.25, and 0.26?

2) do you want to get something like this:

data have;
input
ID 	Year 	Size $	Return 	;
cards;
1	2016	sml	-2.1	 
1	2017	sml	0.25	 
1	2018	med	0.26	
2	1999	sml	0.5	 
2	2000	med	-0.8	 
2	2001	med	0.7	 
2	2002	med	1.9	
;
run;
proc sort data = have;
  by ID year;
run;

data want;

  set have;
  by id;

  if first.ID then _avg = .;
  _avg + return;
  _n + (return>.z);

  if last.id then average = _avg/_n;
  drop _:;

  put _all_;
run;
proc print;
run;

 

Bart

 

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



Ksharp
Super User
data have;
input
ID 	Year 	Size $	Return 	;
cards;
1	2016	sml	-2.1	 
1	2017	sml	0.25	 
1	2018	med	0.26	
2	1999	sml	0.5	 
2	2000	med	-0.8	 
2	2001	med	0.7	 
2	2002	med	1.9	
;
run;
proc sql;
create table temp as
select *,mean(return) as mean from have group by id;
quit;
data want ;
 set temp;
 by id;
 if not last.id then call missing(mean);
run;

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!

Submit your idea!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 652 views
  • 2 likes
  • 4 in conversation