【问题标题】:How to use a value that comes from a different table?如何使用来自不同表的值?
【发布时间】:2021-03-23 15:07:32
【问题描述】:

我创建了一个数据集来计算阈值:

Data black;
Set blue;
Lower=p20-2;
Upper=p20+2;
Run;

我想使用这个值,输出是这样的:

Variables n  lower upper
Val      123  -0.2  0.1

我想使用上限和下限作为阈值:

Proc sql;
Create table one as
Select * from two
Where (Val < upper and Val > lower)
;quit;

Upperlower 应该来自black,而Val 应该来自twotwo 看起来像

ID Val
42 1471
32 1742
74 4819
...

如何在我的数据集中包含阈值以过滤来自two 的值?

一种可能的解决方案是将较低的值和较高的值添加到两列,但我知道如何将值分配给这些列。

【问题讨论】:

    标签: sas proc-sql


    【解决方案1】:

    如果所有行的边界都是静态的,则可以将它们读入宏变量并在 SQL 查询中引用它们。

    data black;
        set blue;
        Lower=p20-2;
        Upper=p20+2;
    
        /* Save the value of lower/upper to macro variables &lower and &upper */
        call symputx('lower', lower);
        call symputx('upper', upper);
    run;
    
    proc sql;
        create table one as
        select * from two
        where &lower. < Val < &upper.
        ;
    quit;
    

    如果每个 ID 都有特定的阈值,您可以使用哈希表按其键查找每个值。哈希表在 SAS 中非常高效,是在大表中查找小表的好方法。

    data two;
        set one;
    
        if(_N_ = 1) then do;
            dcl hash h(dataset: 'blue');
                h.defineKey('id');
                h.defineData('lower', 'upper');
            h.defineDone();
    
            /* Initialize lower/upper to missing */       
            call missing(lower, upper);
        end;
    
        /* Take the ID for the current row in the and look it up in hash table. 
           If there is a match, return the value of lower/upper for that ID */
        rc = h.Find();
    
        /* Only output if the ID is between its threshold */
        if(&lower. < Val < &upper.);
       
        drop rc;
    run;
    

    如果您更喜欢使用 SQL,您可以使用未记录的 magic=103 选项操纵 SQL 优化器来强制进行哈希联接。在较小的表上进行连接有时会更有效。

    proc sql magic=103;
        create table two as
            select t1.*
            from one as t1
            LEFT JOIN
                black as t2
            ON t1.id = t2.id
            where t2.lower < t1.val < t2.upper
        ;
    quit;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-17
      • 1970-01-01
      • 2013-02-26
      • 1970-01-01
      相关资源
      最近更新 更多