BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
NKormanik
Barite | Level 11

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!

1 ACCEPTED SOLUTION

Accepted Solutions
Linlin
Lapis Lazuli | Level 10

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

9 REPLIES 9
Reeza
Super User

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;

NKormanik
Barite | Level 11

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

Bummer.

Reeza
Super User

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.

NKormanik
Barite | Level 11

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.

Reeza
Super User

See above Smiley Happy

Edited the original response.

NKormanik
Barite | Level 11

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;

Linlin
Lapis Lazuli | Level 10

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

NKormanik
Barite | Level 11

Nice form, Linlin.  Thanks much!

Reeza
Super User

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

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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