<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Nested if - do loops in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Nested-if-do-loops/m-p/442289#M110647</link>
    <description>&lt;P&gt;Found it, just had to extend the array with appropriate variables. Ty.&lt;/P&gt;</description>
    <pubDate>Mon, 05 Mar 2018 10:09:21 GMT</pubDate>
    <dc:creator>89974114</dc:creator>
    <dc:date>2018-03-05T10:09:21Z</dc:date>
    <item>
      <title>Nested if - do loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Nested-if-do-loops/m-p/442280#M110640</link>
      <description>&lt;P&gt;I have a dataset which looks like this:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;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&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I am looking to create an array based on a few inputs:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;%let FixedorFloating = '1 or 0';
%let Repricingfrequency = n Years;
%let LastRepricingDate = 'Date'n;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;So far my code looks like this:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;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 &amp;amp;FixedorFloating=1;

    do i=&amp;amp;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 &amp;amp;fixedorfloating=0;

    do i=&amp;amp;dateoflastrepricing to hbound(_year);
    if put (ID,4.)&amp;lt;=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;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;The code works for the original if function but I'd like to make this more dynamic by introducing the else if FixedorFloating=0.&lt;/P&gt;&lt;P&gt;I'm also looking to make my function able to decipher whether the ID is on a year +2i year from the ID. i.e.&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;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)&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I'm unsure if this is reasonable or incorrect.&lt;/P&gt;&lt;P&gt;The error of the log looks like this:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;82         else if &amp;amp;fixedorfloating=0;
         ____
         160
 ERROR 160-185: No matching IF-THEN clause.

 84         then do i=&amp;amp;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.&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I'm assuming the if do followed by an else-if do isn't structured properly.&lt;/P&gt;</description>
      <pubDate>Mon, 05 Mar 2018 09:46:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Nested-if-do-loops/m-p/442280#M110640</guid>
      <dc:creator>89974114</dc:creator>
      <dc:date>2018-03-05T09:46:11Z</dc:date>
    </item>
    <item>
      <title>Re: Nested if - do loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Nested-if-do-loops/m-p/442284#M110643</link>
      <description>&lt;P&gt;An if statement like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if &amp;amp;FixedorFloating=1;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;(if without a then)&lt;/P&gt;
&lt;P&gt;is called a "subsetting if", it is standalone, and does not support an else clause.&lt;/P&gt;
&lt;P&gt;I guess you wanted something along:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;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 &amp;amp;FixedorFloating=1
then do i=&amp;amp;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 &amp;amp;fixedorfloating=0
then do i=&amp;amp;dateoflastrepricing to hbound(_year);
  if put (ID,4.)&amp;lt;=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;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 05 Mar 2018 09:56:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Nested-if-do-loops/m-p/442284#M110643</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-03-05T09:56:52Z</dc:date>
    </item>
    <item>
      <title>Re: Nested if - do loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Nested-if-do-loops/m-p/442288#M110646</link>
      <description>&lt;P&gt;Perfect, thanks.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Just trying to figure out a way to make the do loop flag up ID = year+2i now.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I get this error:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;ERROR: Array subscript out of range at line 87 column 43.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Looking to pair up based on the following:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If ID = 2017, flag 2017, 2019&lt;/P&gt;&lt;P&gt;if ID= 2018, flag 2018, 2020&lt;/P&gt;&lt;P&gt;if ID= 2019, flag 2019&lt;/P&gt;&lt;P&gt;if ID= 2020, flag 2020&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 05 Mar 2018 10:05:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Nested-if-do-loops/m-p/442288#M110646</guid>
      <dc:creator>89974114</dc:creator>
      <dc:date>2018-03-05T10:05:25Z</dc:date>
    </item>
    <item>
      <title>Re: Nested if - do loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Nested-if-do-loops/m-p/442289#M110647</link>
      <description>&lt;P&gt;Found it, just had to extend the array with appropriate variables. Ty.&lt;/P&gt;</description>
      <pubDate>Mon, 05 Mar 2018 10:09:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Nested-if-do-loops/m-p/442289#M110647</guid>
      <dc:creator>89974114</dc:creator>
      <dc:date>2018-03-05T10:09:21Z</dc:date>
    </item>
  </channel>
</rss>

