尽管返回非零,但哈希查找方法输入值 - SAS [英] Hash find method entering values despite returning non-zero - SAS
问题描述
我在数据步骤中使用哈希进行查询,如下所示:
I have a query using hashes in a data step as follows:
if _n_ = 1 then do;
if 0 then
set TableA;
declare hash A(dataset: "TableA");
A.definekey ("UniqueKeyA" );
A.definedata ("DataA" );
A.definedone();
end;
if _n_ = 1 then do;
if 0 then
set TableB;
declare hash B(dataset: "TableB");
B.definekey ("UniqueKeyB" );
B.definedata ("DataB" );
B.definedone();
end;
rcA = A.find(key:UniqueKeyA);
rcB = B.find(key:UniqueKeyB);
我的印象是,如果数据存在,find
方法返回 0,否则返回非零,如果找到数据,它会将其放入表中.但是,这些查询为 rcA
或 rcB
返回非零值,并且仍在输入值.
I'm under the impression that the find
method returns 0 if the data exists, non-zero otherwise, and that if data is found it puts it into the table. However, these queries are returning non-zero values for rcA
or rcB
and still putting in values.
明确地说,如果我的原始表是
Explicitly, if my original table is
UniqueKeyA UniqueKeyB
1 A
2 B
3 C
4 D
5 E
和TableA
是
UniqueKeyA DataA
1 'High'
2 'Low'
5 'High
和'TableB是
UniqueKeyB DataA
B 'Hot'
D 'Cold'
E 'Warm'
结果表可能是
UniqueKeyA UniqueKeyB rcA DataA rcB DataB
1 A 0 'High' 42
2 B 0 'Low' 0 'Hot'
3 C 45 'Low' 42 'Hot'
4 D 45 'Low' 0 'Cold'
5 E 0 'High 0 'Warm'
我现在有一个工作
if rcA = 0 then DataA = DataA;
if rcA ^= 0 then DataA = "";
if rcB = 0 then DataB = DataB;
if rcB ^= 0 then DataB = "";
但这似乎没有必要,因为我认为 find
只有在返回 0 时才会写入表.
But this seems unnecessary, since I thought find
only writes to the table if it returns 0.
有人知道为什么会发生这种情况吗?
Does anyone know why this might be happening?
如果您需要更多信息,请告诉我,谢谢!
Please let me know if you need any more information, thanks!
另外,我还有一个关于这些哈希返回整个表而不是指定列的问题,问题在这里:散列返回整个表 - SAS
Also, I have another question about these hashes returning the entire table rather than the specified column, the question is here: Hash returning entire table - SAS
推荐答案
这是同一个东西的另一个副作用 - if 0 then set tableA
.
This is another side effect of the same thing - if 0 then set tableA
.
任何通过SET
、MERGE
或UPDATE
语句到达数据步骤的变量将自动RETAIN
ed,这意味着它不会在数据步循环开始时设置为缺失.所以你要么需要放弃 if 0 然后设置 tableA/B
行(你可以用几个 length
语句替换它们,每个散列中的每个变量一个)或者做你在做什么,手动将它们设置为缺失.
Any variable that arrives in the data step through a SET
, MERGE
, or UPDATE
statement will be automatically RETAIN
ed, which means that it won't be set to missing at the start of the data step loop. So you either need to abandon the if 0 then set tableA/B
lines (you can replace them with a couple of length
statements, one per variable in each hash) or do what you're doing, set them to missing manually.
这篇关于尽管返回非零,但哈希查找方法输入值 - SAS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!