创建新变量,然后根据新变量进行过滤

Create new variable and then filter according to the new variable

我最近才开始使用SAS。我正在尝试基于另一个 table oldtable 创建一个新的 table newtable。 假设 oldtable 包含变量 OldPrice。我想用基于 OldPrice 计算的新变量 NewPrice 创建 newtable。然后过滤 newtable 以仅显示大于 10.

NewPrice

下面是我的示例代码。

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

但是,我收到错误消息说 NewPrice 不是 oldtable 的变量。

WHERE 在数据到达数据步骤之前对其进行操作。要有条件地删除已经在数据步骤中的观察,您需要使用 IF.

例如,只需使用子集 IF 而不是 WHERE。

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

或明确删除您不想要的观察结果。

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

WHERE 语句表达式只能使用来自 SET 语句的程序数据向量 (PDV) 中的变量。

数据集选项 WHERE= 可用于指定输入或输出数据集。

声明

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

选项

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

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