<?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: looping if then else if in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/looping-if-then-else-if/m-p/823298#M325078</link>
    <description>&lt;P&gt;You have 'week' as the designated interval for FREQ='F', and month for the other listed FREQ values.&amp;nbsp; So you have to change two parameters in the intnx function.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, there is no need within the code you submitted to generate date1, since canceldate can be used in the function calls:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Below is untested in the absence of sample data:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test2 (drop=_:);
  set test;

  if freq in ('M','H','Q','F') then do;
    _parm2=index('MFQ..H',freq); /* M-&amp;gt;1, F-&amp;gt;2, Q-&amp;gt;3, H-&amp;gt;6 */
    _parm1='month';
    if freq='F' then _parm1='week';
  end;
  days2=intnx(_parm1,canceldate,_parm2,0);
  if days &amp;lt; days2 then do;
    prem=round(days/days2*prem1,0.01);
    calc=round(days/days2*calc1,0.01);
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sat, 16 Jul 2022 01:34:11 GMT</pubDate>
    <dc:creator>mkeintz</dc:creator>
    <dc:date>2022-07-16T01:34:11Z</dc:date>
    <item>
      <title>looping if then else if</title>
      <link>https://communities.sas.com/t5/SAS-Programming/looping-if-then-else-if/m-p/823265#M325064</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;can the below code be written in small and efficient way.basically can we eliminate the repetitive code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test2;
set test;

if freq='M' then
do;
date1=canceldate;
days2=intnx('month',date1,1)-intnx('month',date1,0);
if days &amp;lt; days2 then
do;
prem=round(days/days2*prem1,0.01);
calc=round(days/days2*calc1,0.01);
end;

end;
else if freq='H' then
do;
date1=canceldate;
days2=intnx('month',date1,6)-intnx('month',date1,0);
if days &amp;lt; days2 then
do;
prem=round(days/days2*prem1,0.01);
calc=round(days/days2*calc1,0.01);
end;

end;

else if freq='Q' then
do;
date1=canceldate;
days2=intnx('month',date1,3)-intnx('month',date1,0);
if days &amp;lt; days2 then
do;
prem=round(days/days2*prem1,0.01);
calc=round(days/days2*calc1,0.01);
end;

end;

else if freq='F' then
do;
date1=canceldate;
days2=intnx('week',date1,2)-intnx('week',date1,0);
if days &amp;lt; days2 then
do;
prem=round(days/days2*prem1,0.01);
calc=round(days/days2*calc1,0.01);
end;

end;

run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 14 Jul 2022 11:51:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/looping-if-then-else-if/m-p/823265#M325064</guid>
      <dc:creator>sfffdg</dc:creator>
      <dc:date>2022-07-14T11:51:18Z</dc:date>
    </item>
    <item>
      <title>Re: looping if then else if</title>
      <link>https://communities.sas.com/t5/SAS-Programming/looping-if-then-else-if/m-p/823266#M325065</link>
      <description>&lt;P&gt;&lt;SPAN&gt;It looks as if the only thing that is changing is the 3rd argument of INTNX. (By the way, you should just come out and say that — tell us what you know — rather than expecting someone to figure that out and possibly get it wrong ... did I miss any other areas of code that is changing?)&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test2;
    set test;
    if freq='M' then parm=1;
    else if freq='H' then parm=6;
    else if freq='Q' then parm=3;
    else if freq='F' then parm=2;
    date1=canceldate;
    days2=intnx('month',date1,parm)-intnx('month',date1,0);
    if days &amp;lt; days2 then do;
        prem=round(days/days2*prem1,0.01);
        calc=round(days/days2*calc1,0.01);
    end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 14 Jul 2022 12:02:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/looping-if-then-else-if/m-p/823266#M325065</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-07-14T12:02:44Z</dc:date>
    </item>
    <item>
      <title>Re: looping if then else if</title>
      <link>https://communities.sas.com/t5/SAS-Programming/looping-if-then-else-if/m-p/823282#M325074</link>
      <description>&lt;P&gt;One issue in producing an equivalent program is this.&amp;nbsp; Is FREQ guaranteed to always be "M", "J", "Q", or "F"?&amp;nbsp; If so,&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;'s suggestion is ideal.&amp;nbsp; If not (for example, if FREQ is sometimes missing),&amp;nbsp; you might want to modify it slightly:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test2;
    set test;
    if freq='M' then parm=1;
    else if freq='H' then parm=6;
    else if freq='Q' then parm=3;
    else if freq='F' then parm=2;
   if freq in ('M', 'H', 'Q', 'F') then do;
     date1=canceldate;
     days2=intnx('month',date1,parm)-intnx('month',date1,0);
     if days &amp;lt; days2 then do;
        prem=round(days/days2*prem1,0.01);
        calc=round(days/days2*calc1,0.01);
     end;
   end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 14 Jul 2022 12:43:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/looping-if-then-else-if/m-p/823282#M325074</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2022-07-14T12:43:25Z</dc:date>
    </item>
    <item>
      <title>Re: looping if then else if</title>
      <link>https://communities.sas.com/t5/SAS-Programming/looping-if-then-else-if/m-p/823288#M325076</link>
      <description>&lt;P&gt;Not sure if this performs better than the suggested solutions (code is not tested):&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc format;
  invalue freq2num
    'M' = 1
    'H' = 6
    'Q' = 3
    'F' = 2
    other = .;
run;

data want;
  set have;
  date1=canceldate;
  days2=intnx('month',date1,input(freq, freq2num.)) - intnx('month',date1,0);

  if days &amp;lt; days2 then do;
    prem=round(days/days2*prem1,0.01);
    calc=round(days/days2*calc1,0.01);
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 14 Jul 2022 13:12:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/looping-if-then-else-if/m-p/823288#M325076</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2022-07-14T13:12:31Z</dc:date>
    </item>
    <item>
      <title>Re: looping if then else if</title>
      <link>https://communities.sas.com/t5/SAS-Programming/looping-if-then-else-if/m-p/823298#M325078</link>
      <description>&lt;P&gt;You have 'week' as the designated interval for FREQ='F', and month for the other listed FREQ values.&amp;nbsp; So you have to change two parameters in the intnx function.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, there is no need within the code you submitted to generate date1, since canceldate can be used in the function calls:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Below is untested in the absence of sample data:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test2 (drop=_:);
  set test;

  if freq in ('M','H','Q','F') then do;
    _parm2=index('MFQ..H',freq); /* M-&amp;gt;1, F-&amp;gt;2, Q-&amp;gt;3, H-&amp;gt;6 */
    _parm1='month';
    if freq='F' then _parm1='week';
  end;
  days2=intnx(_parm1,canceldate,_parm2,0);
  if days &amp;lt; days2 then do;
    prem=round(days/days2*prem1,0.01);
    calc=round(days/days2*calc1,0.01);
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 16 Jul 2022 01:34:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/looping-if-then-else-if/m-p/823298#M325078</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2022-07-16T01:34:11Z</dc:date>
    </item>
    <item>
      <title>Re: looping if then else if</title>
      <link>https://communities.sas.com/t5/SAS-Programming/looping-if-then-else-if/m-p/823327#M325082</link>
      <description>&lt;P&gt;Editted comment.&amp;nbsp; Just realized that I submitted this response twice, so I'm striking out the entire content below:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRIKE&gt;It looks like you want WEEK interval for FREQ=-'F' and MONTH for the others.&amp;nbsp; So you have to modify two parameters of the INTNX function.&lt;/STRIKE&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRIKE&gt;Also there is no apparent need for the DATE1 variable in you code, since CANCELDATE is identical.&lt;/STRIKE&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRIKE&gt;The code below is untested:&lt;/STRIKE&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;STRIKE&gt;&lt;CODE class=" language-sas"&gt;data test2 (drop=_:);
  set test;

  if freq in ('M','H','Q','F') then do;
    _parm2=index('MFQ..H',freq); /* M-&amp;gt;1, F-&amp;gt;2, Q-&amp;gt;3, H-&amp;gt;6 */
    _parm1='month';
    if freq='F' then _parm1='week';
  end;
  days2=intnx(_parm1,canceldate,_parm2,0);
  if days &amp;lt; days2 then do;
    prem=round(days/days2*prem1,0.01);
    calc=round(days/days2*calc1,0.01);
  end;
run;&lt;/CODE&gt;&lt;/STRIKE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 15 Jul 2022 11:05:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/looping-if-then-else-if/m-p/823327#M325082</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2022-07-15T11:05:13Z</dc:date>
    </item>
  </channel>
</rss>

