【问题标题】:Create new variable and then filter according to the new variable创建新变量,然后根据新变量过滤
【发布时间】:2022-01-07 02:24:10
【问题描述】:

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

下面是我的示例代码。

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

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

【问题讨论】:

    标签: variables sas


    【解决方案1】:

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

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

    声明

    数据想要;
    设置拥有表达式;
    ...
    

    选项

    数据想要;
    设置(where=(表达式));
    ...
    

    数据想要(where=(表达式));
    设置拥有;
    ...
    

    【讨论】:

      【解决方案2】:

      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;   
      

      【讨论】:

        猜你喜欢
        • 2019-08-18
        • 1970-01-01
        • 2018-07-02
        • 1970-01-01
        • 1970-01-01
        • 2015-09-24
        • 1970-01-01
        • 1970-01-01
        • 2021-07-24
        相关资源
        最近更新 更多