嵌套 If do 语句 SAS

Nested If do statements SAS

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

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 的结构不正确。

问题在这里:

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

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

尝试更改为:

if &FixedorFloating=1 then
  do i=&dateoflastrepricing to hbound(_year);
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;

作者:KurtBremser