debster
debster

Reputation: 333

Create new variable and then filter according to the new variable

I have just started using SAS recently. I am attempting to create a new table newtable based on another table oldtable. Let's say the oldtable contains variable OldPrice. I want to create newtable with a new variable NewPrice calculated based on OldPrice. Then filter the newtable to only show NewPrice which is larger than 10.

Below is the sample code I have.

data newtable;
set oldtable;
NewPrice = OldPrice * 2
where NewPrice > 10;
run;

However, I receive error message saying that NewPrice is not a variable of oldtable.

Upvotes: 0

Views: 784

Answers (2)

Tom
Tom

Reputation: 51566

WHERE operates on the data before it reaches the data step. To conditional delete an observation once it is already in a data step you need to use IF.

For exameple just use a subsetting IF instead of WHERE.

data newtable;
  set oldtable;
  NewPrice = OldPrice * 2;
  if NewPrice > 10;
run;

Or explicitly delete the observations you don't want.

data newtable;
  set oldtable;
  NewPrice = OldPrice * 2;
  if NewPrice <= 10 then delete;
run;   

Upvotes: 1

Richard
Richard

Reputation: 27498

A WHERE statement expression can only use variables in the program data vector (PDV) that come from a SET statement.

The data set option WHERE= can be used in the specification of an input or output data set.

Statement

data want;
  set have;
  where expression;
  ...

Option

data want;
  set have(where=(expression));
  ...

or

data want(where=(expression));
  set have;
  ...

Upvotes: 2

Related Questions