【问题标题】:Hash table where statement哈希表 where 语句
【发布时间】:2021-08-16 10:19:52
【问题描述】:

我正在尝试将哈希对象用作类似 sql 查询的东西。

我想知道的是相当于 SAS 哈希对象的条件 if 语句。

我想要一个基于有效日期过滤的第二个哈希对象的条件语句。

类似

If effective_date<"27.05.1966"'d then output.

rc=h.find() 根据是否找到返回 0/1,但是如何将 if 语句或 where 语句添加到哈希对象?

我在想类似的事情

if rc=0 and effective_date<"27.05.1966"'d then do;
set want;
hoh.add();
output;
end;

但我认为哈希表不会知道 Effective_date 是以这种方式被引用的。

我要翻译的原始 proc sql 语句:


proc sql;
    create table tb2 as
        select distinct
            a.effective_date,
            a.S_FACILITY_CUSTOMER_ID as customer_id,
            a.s_facility_id as facility_id,
            a.s_facility_type as facility_type,
            a.CUSTOMER_ASSET_CLASS_ID,
            a.FACILITY_START_DATE,
            a.FACILITY_end_DATE,
            sum
            (
        case 
            when midas_type_id in ("KC", "KR", "KO","KF") then sum(a.loan_prinicpal_local,a.loan_interest_unpaid_local)
            else 0 
        end
                )
            as arrear_amount,

sum(a.LOAN_PRINICPAL_LOCAL) as amount_local
            from tbexport.tb_export_full_all a
                where a.effective_date between "&repdate_from"dt and "&repdate_to"dt

                    AND BUSINESS_SEGMENT NOT IN ("MCR", "SE", "COR")
                group by s_facility_id, effective_date;
quit;

这是我想出的:

data _NULL_;

if 0 then set tbexport.tb_export_full_all;

   dcl hash HoH(ordered : 'D');                                                    
   HoH.definekey('s_facility_id','effective_date');
   HoH.definedata('effective_date', 'S_FACILITY_CUSTOMER_ID', 's_facility_type','s_facility_id','CUSTOMER_ASSET_CLASS_ID',
'FACILITY_START_DATE','FACILITY_end_DATE','midas_type_id','BUSINESS_SEGMENT','loan_prinicpal_local','loan_interest_unpaid_local');
   HoH.definedone();
   dcl hiter HoHiter('HoH');

  do until (lr);
      set tbexport.tb_export_full_all end=lr;
        where '&repdate_from.'dt<=effective_date<='&repdate_to.'dt;
        if HoH.find() ne 0 then do;  
 
         dcl hash h(multidata : 'Y', ordered : 'D');
         h.definekey('effective_date','s_facility_id');
         h.definedata('effective_date', 'S_FACILITY_CUSTOMER_ID', 's_facility_type','s_facility_id','CUSTOMER_ASSET_CLASS_ID',
'FACILITY_START_DATE','FACILITY_end_DATE','midas_type_id','BUSINESS_SEGMENT','loan_prinicpal_local','loan_interest_unpaid_local');
         h.definedone();
         dcl hiter hi('h');
         HoH.add();

        end;    
      h.add();
   end;

   do until(HoHiter.next() = 1);  
        set tbexport.tb_export_full_all; 
        if hoh.find()=0 and midas_type_id in ("KC" , "KR" , "KO" , "KF") then do;    
        arrear_amount= sum(loan_prinicpal_local,loan_interest_unpaid_local);
        end;
      hoh.output(dataset:'tb2',ordered:'A');
   end;

run;

但它给了我这个错误:

错误:遇到异常。 请联系技术支持并向他们提供以下追溯信息:

SAS 任务名称是 [DATASTEP] 分割违规

【问题讨论】:

  • 当你测试一个变量时,你就是在测试它的当前值。因此,您可能想在 SET 语句之后进行测试,否则您要测试什么值?如果 find() 成功,则 DEFINEDATA() 方法定义的变量将使用从哈希对象检索到的值进行更新,因此您可以测试这些值。但是您在发布的代码中没有对日期文字使用有效的语法。您有不平衡的引号,并且引号中的字符串不是 DATE 信息可以翻译的内容。
  • 请解释您要编写的代码。 “sql 查询”过于笼统,无法解释您要复制的查询类型。
  • @Tom 根据日期从较大的数据库表中进行简单子集化,如果满足条件则为总和。我会将确切的 proc sql 语句添加到我的帖子中。
  • @Tom 那么“某事”是否优于“某事”?
  • 日期文字是一个带引号的字符串,在右引号后带有字母 d。引号中的值需要是 DATE 信息可以转换为日期的字符串。示例:"01JAN1960"d

标签: sas hashtable


【解决方案1】:

这应该可以在没有哈希值的情况下实现。这大致复制了您正在做的事情 - 通过几个变量创建摘要。我认为在这一点上添加逻辑非常简单 - 它可能是where 语句。只需确保将其添加到两个数据集读取(可能最好作为where 数据集选项,也许?)。

data want;
  if 0 then set sashelp.prdsale;
  
  if _n_ eq 1 then do;
    declare hash h();
    h.defineKey('country','region');
    h.defineData('actual_sum','predict_sum');
    h.defineDone();    
      do _n_ = 1 to prdsale_recs;
        set sashelp.prdsale nobs=prdsale_recs point=_n_;
        rc = h.find();
        actual_sum = sum(actual_sum,actual);
        predict_sum = sum(predict_sum,predict);
        rc = h.replace();       
      end;
  end;  
  set sashelp.prdsale;

  rc = h.find();
run;

【讨论】:

  • 我真的希望通过这个例子学习如何使用 HoH,但你的方法是完全有效的。不过,我真的不明白它是如何选择不同的组合的。
  • 表格也未排序,所以我想我确实需要 HoH。
  • 你的意思是你的输入表吗?
  • 是的,我的意思是输入表。
  • 为什么你认为 HoH 需要用于这两个目的?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-30
  • 2022-11-16
  • 2015-05-25
  • 2023-03-31
  • 1970-01-01
相关资源
最近更新 更多