Subset observations based on standard deviation

Accepted Solution Solved
Reply
Regular Contributor
Posts: 212
Accepted Solution

Subset observations based on standard deviation

Given a single column data set, continuous, 10,000 rows.

Create a sub-set that meets the following conditions:

--- Elements greater than one standard deviation above the mean.

--- But less than two standard deviations above the mean.

Prefer to use basic SAS functions.

data nicholas.between_1sd2sd;

set nicholas.combined;

where...

;

run;

Thanks!


Accepted Solutions
Solution
‎01-10-2013 07:54 PM
Super Contributor
Posts: 1,636

Re: Subset observations based on standard deviation

is the example helpful?

proc sql noprint;;

  select mean(age) into : mean from sashelp.class;

  select std(age) into :std from sashelp.class;

quit; 

data class;

  set sashelp.class;

   where age >&mean+&std and age<&mean+2*&std;

proc print;run;

              Obs    Name       Sex    Age    Height    Weight

                  1     Janet       F      15     62.5      112.5

                  2     Mary        F      15     66.5      112.0

                  3     Philip      M      16     72.0      150.0

                  4     Ronald      M      15     67.0      133.0

                  5     William     M      15     66.5      112.0

View solution in original post


All Replies
Super User
Posts: 17,899

Re: Subset observations based on standard deviation

This is almost identical to your previous question with percentiles instead of standard deviation.

You can use the same code, except replace it with standard deviation.

The datastep goes through data one row at a time so you need to have the standard deviation pre-calculated before you can use the where clause.

A SQL procedure operates on the entire column at once and can do it in one step, or at least one coded step, though it may take multiple steps behind the scenes.

proc sql;

    create table want as

    select weight, std(weight) as std_weight, avg(weight) as avg_weight

    from sashelp.class

    having abs(weight-calculated avg_weight)/calculated std_weight between 1 and 2;

quit;

Regular Contributor
Posts: 212

Re: Subset observations based on standard deviation

Hmmm.  So the standard deviation function is a row-only one as well?

Bummer.

Super User
Posts: 17,899

Re: Subset observations based on standard deviation

The data step operates on a row by row basis.  All functions are row only, procedures operate across a dataset.

You can however use SQL functions for this one (see above). SQL doesn't have median/order statistics in this implementation.

Regular Contributor
Posts: 212

Re: Subset observations based on standard deviation

Reeza, hate to bother, but would you please put the SQL code for achieving the above task?  Maybe others too will find it here and appreciate having access to it.

Super User
Posts: 17,899

Re: Subset observations based on standard deviation

See above Smiley Happy

Edited the original response.

Regular Contributor
Posts: 212

Re: Subset observations based on standard deviation

Sorta fits the bill, Reeza.  Problem would be we want only the up side here:

Create a sub-set of data set that meets the following conditions:

--- Elements greater than one standard deviation above the mean.

--- But less than two standard deviations above the mean.

Your code give both sides of the curve.

Additionally, and here's the larger problem, while we want to focus on one particular column (50501'n), we want to retain all the additional columns in the original data set (nicholas._21603_).  (Sorry for not making this clear at the outset.)

Here's the code I used so far.  Please edit it as opposed to your example.

proc sql;

create table nicholas.between_plus_1sd2sd as

select '50501'n, std('50501'n) as std_50501, avg('50501'n) as avg_50501

from nicholas._21603_

having abs('50501'n - calculated avg_50501) / calculated std_50501 between 1 and 2;

quit;

Solution
‎01-10-2013 07:54 PM
Super Contributor
Posts: 1,636

Re: Subset observations based on standard deviation

is the example helpful?

proc sql noprint;;

  select mean(age) into : mean from sashelp.class;

  select std(age) into :std from sashelp.class;

quit; 

data class;

  set sashelp.class;

   where age >&mean+&std and age<&mean+2*&std;

proc print;run;

              Obs    Name       Sex    Age    Height    Weight

                  1     Janet       F      15     62.5      112.5

                  2     Mary        F      15     66.5      112.0

                  3     Philip      M      16     72.0      150.0

                  4     Ronald      M      15     67.0      133.0

                  5     William     M      15     66.5      112.0

Regular Contributor
Posts: 212

Re: Subset observations based on standard deviation

Nice form, Linlin.  Thanks much!

Super User
Posts: 17,899

Re: Subset observations based on standard deviation

Adding in an * that will bring in all of the variables, rather than just your variable.

You can modify the where clause to get the upside (above only), your basically looking for z scores between 1 and 2 .

proc sql;

    create table want as

    select *, std(weight) as std_weight, avg(weight) as avg_weight

    from sashelp.class

    having abs(weight-calculated avg_weight)/calculated std_weight between 1 and 2;

quit

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 9 replies
  • 1165 views
  • 4 likes
  • 3 in conversation