【问题标题】:SAS Create a counter that depends by amountSAS 创建一个取决于数量的计数器
【发布时间】:2021-01-27 12:26:29
【问题描述】:

我需要在 SAS 中创建一个计数器,它必须计算是否是同一协议、同一天,这取决于是贷方还是借方以及借方或贷方的金额。它们必须是动态的,因为我不知道同一天有不同数量的时间。所以,这可能是使用数组的解决方案......但我不知道该怎么做。

您可以在下面找到我需要的计数器:Count credit 和 Count debit。

data have;
infile datalines delimiter=',';
informat Agreement $char4. Effective_Date1 ddmmyy10. Debit Credit;
format Agreement $char4. Effective_Date1 ddmmyy10. Debit Credit;
input Agreement Effective_Date1 Debit Credit;
datalines;
A1,01/02/2020,100,0  
A1,01/02/2020,632,0  
A1,01/02/2020,0, 100     
A1,01/02/2020,0,632
A1,01/02/2020,100,0
A1,01/02/2020,632,0  
A1,01/02/2020,0,3    
A1,01/02/2020,3,0    
A1,22/02/2020,3,0    
A2,02/03/2020,50,0
;;

非常感谢!

问候,

【问题讨论】:

    标签: sql count sas counter


    【解决方案1】:

    这可以通过使用哈希表来存储查找中的值来实现。请看下面的实现。

    结果与您的示例输出不太一样,因为 3 借方在当天早些时候已经被视为 3 贷方,这在输出中表示,与您示例表中的输出一致。

    Proc Sort Data=have; 
     by Agreement Effective_Date1;
    run;
    
    data want (drop=rc); 
    
     set have; 
     
     attrib 
        count_credit 
        count_debit
        amount
                    length=8.; 
    
         
     by Agreement Effective_Date1;
      
     if _n_ = 1 then do; 
        *declare hash table to keep track of what has been seen and how 
            many times as a credit or a debit; 
        declare hash seen_d();
        seen_d.defineKey('amount');
        seen_d.defineData('count_debit', 'count_credit');
        seen_d.defineDone(); 
     end; 
      
      
     if first.effective_date1 then do; 
        *clear counters and hash table at the beginning of the by group; 
        count_credit = 0; 
        count_debit  = 0; 
        amount       = 0;
        
        rc=seen_d.clear();
     end;
     
     *Assume that only a credit OR debit can exist on a given row;
     amount = max(credit, debit);
     
     rc=seen_d.find();
     
     if rc=0 then do; 
        *if amount found in hash then increment the relevant counter
         and update the entry in the hash table; 
        count_credit = count_credit + (credit > 0);
        count_debit = count_debit +   (debit  > 0);
        seen_d.replace();
      end; 
      else do; 
        *if not found set the relevant counter and add to the hash table; 
        count_credit = (credit > 0);
        count_debit =  (debit > 0);
        seen_d.add();
      end; 
     Run;
    

    【讨论】:

    • 这对您的问题有帮助吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多