<?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: if then do nested within %do %end in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/if-then-do-nested-within-do-end/m-p/372795#M276132</link>
    <description>&lt;P&gt;Thar makes sense. I figure out a way to solve my problem just now, it works but maybe regular do end also work:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro change;
%local n;
data two_inter;
   set two;
   %do n = 0 %to 23;
   %let m = %eval(&amp;amp;n+1);
   if start_hour = &amp;amp;n. then do
   inter&amp;amp;n = 60 - minute(start_time);
   inter&amp;amp;m = minute(stop_time);
   end;
   %end;
   ;
run;
%mend change;
%change;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Mon, 03 Jul 2017 17:31:35 GMT</pubDate>
    <dc:creator>panda</dc:creator>
    <dc:date>2017-07-03T17:31:35Z</dc:date>
    <item>
      <title>if then do nested within %do %end</title>
      <link>https://communities.sas.com/t5/SAS-Programming/if-then-do-nested-within-do-end/m-p/372791#M276130</link>
      <description>&lt;P&gt;Hi -&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to write a code nested if then do end within the %do and %end macro, not sure how to make it work, right now my code looks like this:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro change;
%local n;
data want;
   set data;
   %do n = 0 %to 23;
   if start_hour = &amp;amp;n. then do
   inter&amp;amp;n = 60 - minute(start_time);
   inter&amp;amp;n+1 = minute(stop_time);
   end;
   %end;
   ;
run;
%mend change;
%change;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;The results that I want is to get the inter0 = 60-minute(start_time); inter1 = minute(stop_time); and same goes with inter1, inter2 &amp;nbsp;etc.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My solution now is to write each one separately in each macro, like this: (but this does not seem to be very efficient)&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro change;
%local n;
data want;
   set one;
   %do n = 0 %to 23;
   if start_hour = &amp;amp;n. then do
   inter&amp;amp;n. = 60 - minute(start_time);
   end;
   %end;
   ;
run;
%mend change;
%change;

%macro change;
%local n;
data one_inter;
   set one_inter;
   %do n = 0 %to 23;
   if stop_hour = &amp;amp;n. then do
   inter&amp;amp;n. = minute(stop_time);
   end;
   %end;
   ;
 run;
 %mend change;
 %change;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Do anyone has any idea?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Mon, 03 Jul 2017 17:04:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/if-then-do-nested-within-do-end/m-p/372791#M276130</guid>
      <dc:creator>panda</dc:creator>
      <dc:date>2017-07-03T17:04:10Z</dc:date>
    </item>
    <item>
      <title>Re: if then do nested within %do %end</title>
      <link>https://communities.sas.com/t5/SAS-Programming/if-then-do-nested-within-do-end/m-p/372793#M276131</link>
      <description>&lt;P&gt;In a data step why not use a regular DO/THEN/END loop?&lt;/P&gt;</description>
      <pubDate>Mon, 03 Jul 2017 17:29:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/if-then-do-nested-within-do-end/m-p/372793#M276131</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-07-03T17:29:40Z</dc:date>
    </item>
    <item>
      <title>Re: if then do nested within %do %end</title>
      <link>https://communities.sas.com/t5/SAS-Programming/if-then-do-nested-within-do-end/m-p/372795#M276132</link>
      <description>&lt;P&gt;Thar makes sense. I figure out a way to solve my problem just now, it works but maybe regular do end also work:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro change;
%local n;
data two_inter;
   set two;
   %do n = 0 %to 23;
   %let m = %eval(&amp;amp;n+1);
   if start_hour = &amp;amp;n. then do
   inter&amp;amp;n = 60 - minute(start_time);
   inter&amp;amp;m = minute(stop_time);
   end;
   %end;
   ;
run;
%mend change;
%change;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 03 Jul 2017 17:31:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/if-then-do-nested-within-do-end/m-p/372795#M276132</guid>
      <dc:creator>panda</dc:creator>
      <dc:date>2017-07-03T17:31:35Z</dc:date>
    </item>
    <item>
      <title>Re: if then do nested within %do %end</title>
      <link>https://communities.sas.com/t5/SAS-Programming/if-then-do-nested-within-do-end/m-p/372796#M276133</link>
      <description>&lt;P&gt;You need to post some example data so we can understand what you are actually trying to do.&lt;/P&gt;
&lt;P&gt;Your first macro is not going to generate SAS to do what you want.&lt;/P&gt;
&lt;P&gt;In particular this line:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;inter&amp;amp;n+1 = minute(stop_time);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Will generate code like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;inter0 +   1 = minute(stop_time);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Which is basically going to setup INTER0 as a count of the number of times that STOP_TIME has a value where the minute number is equal to 1.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Perhaps you meant to write:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;inter&amp;amp;n = 60 - minute(start_time);
inter%eval(&amp;amp;n+1) = minute(stop_time);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But the problem with that is that then the result of your %DO would be to generate assignment statements that look like they might overwrite each other.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;inter0 = 60 - minute(start_time);
inter1 = minute(stop_time);
inter1 = 60 - minute(start_time);
inter2 = minute(stop_time);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But I think the end result of this fixed macro code would really be the same thing you would get if you just used a program like this without any macro code.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  array inter (0:24) inter0-inter24 ;
  if 0&amp;lt;= start_hour &amp;lt;= 23 then do;
    inter(int(start_hour))=60 - minute(start_time);
    inter(int(start_hour)+1)=minute(stop_time);
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Which is going to generate a strange set of mainly missing variables like:&lt;/P&gt;
&lt;PRE&gt;         s           s
         t      s    t
         a      t    a
         r      o    r                                    i  i  i  i  i  i  i  i  i  i  i  i  i  i  i
         t      p    t   i   i   i   i  i  i  i  i  i  i  n  n  n  n  n  n  n  n  n  n  n  n  n  n  n
         _      _    _   n   n   n   n  n  n  n  n  n  n  t  t  t  t  t  t  t  t  t  t  t  t  t  t  t
         t      t    h   t   t   t   t  t  t  t  t  t  t  e  e  e  e  e  e  e  e  e  e  e  e  e  e  e
 O       i      i    o   e   e   e   e  e  e  e  e  e  e  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r
 b  i    m      m    u   r   r   r   r  r  r  r  r  r  r  1  1  1  1  1  1  1  1  1  1  2  2  2  2  2
 s  d    e      e    r   0   1   2   3  4  5  6  7  8  9  0  1  2  3  4  5  6  7  8  9  0  1  2  3  4

 1  1   0:30   0:40  0  30  40   .   .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
 2  2   2:22   5:55  2   .   .  38  55  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 03 Jul 2017 17:31:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/if-then-do-nested-within-do-end/m-p/372796#M276133</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-07-03T17:31:58Z</dc:date>
    </item>
  </channel>
</rss>

