Arrays 嵌套If-do语句

Arrays 嵌套If-do语句,arrays,loops,if-statement,sas,sas-studio,Arrays,Loops,If Statement,Sas,Sas Studio,我有一个如下所示的数据集: 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 data ReferenceRateContract; set refratecontract; *arrays

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

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
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;
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.
我希望根据以下几个输入创建一个数组:

%let FixedorFloating = '1 or 0';
%let Repricingfrequency = n Years;
%let LastRepricingDate = 'Date'n;
到目前为止,我的代码如下所示:

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
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;
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.
我不确定这是合理的还是不正确的

日志的错误如下所示:

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
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;
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
是“选通if”,这意味着只处理与条件匹配的记录

尝试更改为:

if &FixedorFloating=1 then
  do i=&dateoflastrepricing to hbound(_year);
数据引用合同;
设定合同;
*年份和旗帜的数组;
阵列年(2017:2020)年2017-2020年;
阵列标志(2017:2020)标志2017-2020;
*环路阵列;
如果固定或浮动=1(&F)
然后做i=&dateoflastricing到hbound(_年);
/*检查变量名称中的年份是否与年份匹配*/
如果put(ID,4.)=压缩(vname(_year(i)),'kd')
然后_标志(i)=1;
else_标志(i)=0;
结束;
如果fixedorfloating=0,则为else(&F)
然后做i=&dateoflastricing到hbound(_年);

如果把(ID,4.)放在正确的位置,我只是在更新你发布的答案。