Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- General Programming
- /
- How to calculate the mean of all the observations ...

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

07-15-2014 04:33 PM

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.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to MrBruce

07-15-2014 04:37 PM

Here's a SQL option:

proc sql;

create table want as

select *, mean(age) as avg_age

from sashelp.class;

quit;

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to MrBruce

07-15-2014 04:43 PM

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;

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to MrBruce

07-16-2014 08:37 AM

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to MrBruce

12-10-2014 08:55 AM

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;