【问题标题】:Nested If do statements SAS嵌套 If do 语句 SAS
【发布时间】:2018-08-12 22:09:20
【问题描述】:

我有一个如下所示的数据集:

ID      2017    2018    2019    2020

2017    30      24      20      18
2018    30      24      20      18
2019    30      24      20      18
2020    30      24      20      18

我希望根据一些输入创建一个数组:

%let FixedorFloating = '1 or 0';
%let Repricingfrequency = n Years;
%let LastRepricingDate = 'Date'n;

到目前为止,我的代码如下所示:

data ReferenceRateContract;
set refratecontract;

*arrays for years and flags;
array _year(2017:2020) year2017-year2020;
array _flag(2017:2020) flag2017-flag2020;

*loop over array;

if &FixedorFloating=1;

    do i=&dateoflastrepricing to hbound(_year);
    /*check if year matches year in variable name*/
    if put(ID, 4.) = compress(vname(_year(i)),, 'kd') 
        then _flag(i)=1;
    else _flag(i)=0;

    end;

else if &fixedorfloating=0;

    do i=&dateoflastrepricing to hbound(_year);
    if put (ID,4.)<=compress(vname(_year(i)),,'kd')
        then _flag(i)=1;

        else if put (ID, 4.) = compress(vname(_year(i-2*i)),, 'kd') 
        then _flag(i)=1;

        else _flag(i)=0;
        end;

drop i;

run;

该代码适用于原始 if 函数,但我想通过引入 else if FixedorFloating=0 来使其更具动态性。

我还希望让我的函数能够破译 ID 是否在 ID 的 +2i 年。即

if ID=2017 - i'd like a 1 for years 2017, 2019. For ID=2018, 
I'd like a 1 for 2018, 2020 and so on hence the 

year(I-2*I)

我不确定这是否合理或不正确。

日志的错误是这样的:

82         else if &fixedorfloating=0;
         ____
         160
 ERROR 160-185: No matching IF-THEN clause.

 84         then do i=&dateoflastrepricing to hbound(_year);
          ____
          180
 ERROR 180-322: Statement is not valid or it is used out of proper order.


 91         else _flag(i)=0;
 92         end;
           ___
           161
 ERROR 161-185: No matching DO/SELECT statement.

我假设 if do 后跟 else-if do 的结构不正确。

【问题讨论】:

    标签: arrays loops if-statement sas sas-studio


    【解决方案1】:

    问题就在这里:

    if &FixedorFloating=1;
      do i=&dateoflastrepricing to hbound(_year);
    

    第一个if 是“gating if”,意思是只处理符合条件的记录。

    尝试更改为:

    if &FixedorFloating=1 then
      do i=&dateoflastrepricing to hbound(_year);
    

    【讨论】:

    • 没错,我只是在更新您发布的答案。
    【解决方案2】:
    data ReferenceRateContract;
    set refratecontract;
    
    *arrays for years and flags;
    array _year(2017:2020) year2017-year2020;
    array _flag(2017:2020) flag2017-flag2020;
    
    *loop over array;
    
    if &FixedorFloating=1
    then do i=&dateoflastrepricing to hbound(_year);
    /*check if year matches year in variable name*/
      if put(ID, 4.) = compress(vname(_year(i)),, 'kd') 
      then _flag(i)=1;
      else _flag(i)=0;
    end;
    
    else if &fixedorfloating=0
    then do i=&dateoflastrepricing to hbound(_year);
      if put (ID,4.)<=compress(vname(_year(i)),,'kd')
      then _flag(i)=1;
      else if put (ID, 4.) = compress(vname(_year(i-2*i)),, 'kd') 
      then _flag(i)=1;
      else _flag(i)=0;
    end;
    drop i;
    run;
    

    库尔特布雷姆瑟

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多