BookmarkSubscribeRSS Feed
MrBruce
Calcite | Level 5

Suppose the 'Grade' variable has 100 observations. I want to calculate the mean of those 100 observations and save the value as one varaible on the table. I know we could easily get the mean through "proc mean", but that will not save the mean value as a new varaible on the same table.

Thanks in advance. 

4 REPLIES 4
Reeza
Super User

Here's a SQL option:

proc sql;

create table want as

select *, mean(age) as avg_age

from sashelp.class;

quit;

stat_sas
Ammonite | Level 13

data have;

input grade;

datalines;

1.4

3.2

3.4

2.1

4.3

5.4

;

proc sql;

select grade,avg(grade) as mean from have;

quit;

Ksharp
Super User

OR, after get mean by proc mean , you can do something like this:

proc mean noprint;

......

output out=mean mean=;

run;

data want;

set have;

if _n_ eq 1 then set mean;

run;

Xia Keshan

user24feb
Barite | Level 11

As an alternative you could loop through your data set yourself and add the mean:

data have;
input grade;
datalines;
1.4
3.2
3.4
2.1
4.3
5.4
;
Run;

Data Want (Drop=Obs i Total);
  Do Obs=1 By 1 Until (Eof);
    Set Have End=Eof;
Total=Sum(Total,Grade);
  End;
  Mean=Total/Obs;
  Do i=1 To Obs;
    Set Have;
Output;
  End;
Run;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 1753 views
  • 0 likes
  • 5 in conversation