Turn on suggestions

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

Showing results for

- Home
- /
- Programming
- /
- SAS Procedures
- /
- Calculating mean, mode, median and STD efficiently

Options

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

🔒 This topic is **solved** and **locked**.
Need further help from the community? Please
sign in and ask a **new** question.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Posted 12-22-2011 03:40 PM
(8335 views)

Hey guys...

I have a dataset like this,

Id Val1 val2 val3 val4 ......valn mean mode median std

1 10 20 30 40...

2 25 13 34 12....

.

.

n

For each row (for each id) i need to find out mean, mode, median and standard deviation and fill the values in the respective columns.

How do I do that efficiently?

1 ACCEPTED SOLUTION

Accepted Solutions

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

3 of these are easy in a datastep;

data new;

set <your data>;

mean = mean (of val1-valn);

median=median (of val1-valn);

std = std (of val1-valn);

/* mode could have issues with tie breakers and such the following selects the first VALi */

/* and no claim to efficiency you may want to drop the c1 c2 i and such*/

array v val1-valn;

c2= 0;

do _i_ = 1 to (dim(v) -1);

c1= 1;

do _j_ = (_i_+1) to dim(v);

c1= c1 + (v[_i_]=v[_j_]);

end;

if c1 gt c2 then do;

c2=c1;

i=_i_;

end;

mode= v*;*

end;

run;

3 REPLIES 3

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

3 of these are easy in a datastep;

data new;

set <your data>;

mean = mean (of val1-valn);

median=median (of val1-valn);

std = std (of val1-valn);

/* mode could have issues with tie breakers and such the following selects the first VALi */

/* and no claim to efficiency you may want to drop the c1 c2 i and such*/

array v val1-valn;

c2= 0;

do _i_ = 1 to (dim(v) -1);

c1= 1;

do _j_ = (_i_+1) to dim(v);

c1= c1 + (v[_i_]=v[_j_]);

end;

if c1 gt c2 then do;

c2=c1;

i=_i_;

end;

mode= v*;*

end;

run;

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

The following doesn't account for tied modes either, but you may find it easier to code:

data have;

input Id Val1 val2 val3 val4 valn;

cards;

1 10 20 20 30 40

2 25 13 34 13 25

;

proc transpose data=have out=long;

var val:;

by id;

run;

proc summary data=long;

var col1;

by id;

output out=want mean=mean median=median std=std mode=mode;

run;

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Thanks guys, it helped!!

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. **Registration is now open through August 30th**. Visit the SAS Hackathon homepage.

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.