Solved
New Contributor
Posts: 2

# Creating a new variable from existing

I feel bad for having to ask for help since I've been trying to teach myself SAS 9.3 but I have run into an issue that I cannot solve the syntax of alone. I am trying to obtain all values of X where X < X - STD(X) without calculating SDT(X) separately and returning all values under that constant value but I think it is much more complicated than I have yet figured out. A simplified version of the code I am trying is displayed below. Thank you for anyone who helps I greatly appreciate it.

data dataset1;

set dataset;

X1 = X < X-STD(X)

run;

The current error I am getting is that STD does not have enough arguments.

Accepted Solutions
Solution
‎03-21-2014 07:43 PM
Super User
Posts: 23,771

## Re: Creating a new variable from existing

The STD function works on a single row of data, since SAS processes data row by row.

You'll have to pre-calculate the STD and merge it in.

Also, the translation for X1=X<X-STD(X) probably isn't what you want and doesn't make sense.

X<x-std(x) == x-x<-std(x)  == 0<-std(x) and std(x) is always positive.

The order of operations will probably mean that x<x will resolve to False or 0 and then subtract std(x) which isn't a valid calculation anyways.

I thought I'd just explain why what you're trying won't work

All Replies
Solution
‎03-21-2014 07:43 PM
Super User
Posts: 23,771

## Re: Creating a new variable from existing

The STD function works on a single row of data, since SAS processes data row by row.

You'll have to pre-calculate the STD and merge it in.

Also, the translation for X1=X<X-STD(X) probably isn't what you want and doesn't make sense.

X<x-std(x) == x-x<-std(x)  == 0<-std(x) and std(x) is always positive.

The order of operations will probably mean that x<x will resolve to False or 0 and then subtract std(x) which isn't a valid calculation anyways.

I thought I'd just explain why what you're trying won't work

New Contributor
Posts: 2

## Re: Creating a new variable from existing

Thank you very much, I was hoping there was a away to calculate it within SAS to save time but I guess not.

Super User
Posts: 23,771

## Re: Creating a new variable from existing

Your question doesn't make sense is my point, it would never evaluate to true. It's very easy to calculate STD within SAS.

proc sql;

create table want as

select a.*, std(weight) as std_weight, std(height) as std_height

from sashelp.class;

quit;

Super User
Posts: 8,120

## Re: Creating a new variable from existing

You might want to look at using SAS/IML (if you have it licensed) if you want to treat your data as if it was a matrix instead of individual observations.

SAS has many tools for calculating statistics like STD.  For example you can use PROC SUMMARY.  Or you can roll your own using PROC SQL.

But perhaps what you want is already available in a PROC?  Did you look at PROC STDIZE?

Super User
Posts: 8,120

## Re: Creating a new variable from existing

It looks like you want to flag the outliers. Here is how you could write a program do that using PROC SUMMARY to find the MEAN and STDDEV of your variable.

* generate some random data for testing ;

data dataset ;

do _n_=1 to 10 ;

x=rand('normal',5,10);

output;

end;

run;

* Find the mean and standard deviation ;

proc summary data=dataset ;

var x;

output out=means(drop=_freq_ _type_) mean= std= /autoname ;

run;

* Combine and create new LOWVALUE and HIVALUE boolean flag variables ;

data want ;

set dataset ;

if _n_=1 then set means ;

lowvalue =  x < x_mean - x_stddev ;

hivalue =  x > x_mean + x_stddev ;

run;

x_Std

Obs           x     x_Mean      Dev      lowvalue    hivalue

1     15.2342    6.79499    10.7221        0          0

2     26.3757    6.79499    10.7221        0          1

3      5.6843    6.79499    10.7221        0          0

4     -3.9084    6.79499    10.7221        0          0

5      8.8976    6.79499    10.7221        0          0

6     -8.4245    6.79499    10.7221        1          0

7      0.2006    6.79499    10.7221        0          0

8     16.1158    6.79499    10.7221        0          0

9     10.2483    6.79499    10.7221        0          0

10     -2.4737    6.79499    10.7221        0          0

🔒 This topic is solved and locked.