嵌套如果做语句SAS

时间:2018-03-05 09:40:41

标签: 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

我希望基于一些输入创建一个数组:

%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来使其更加动态,如果FixedorFloating = 0。

我还希望让我的功能能够破译身份证是否在ID的2年+ 2年内。即

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后面跟着一个其他的 - 如果没有正确的结构。

2 个答案:

答案 0 :(得分:1)

问题在于:

const
  Tab : string = Chr( 9 );

resourcestring
      xmlversion = Tab + '<?xml version="1.0" encoding="utf-8" ?>';
      codetemplate = Chr( 9 ) + '<codetemplate xmlns="http://schemas.borland.com/Delphi/2005/codetemplates" version="1.0.0">';

第一个if &FixedorFloating=1; do i=&dateoflastrepricing to hbound(_year); 是&#34; gating if&#34;,表示只处理与条件匹配的记录。

尝试更改为:

if

答案 1 :(得分:0)

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