<?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: How to understand this consecutive if loop embedded %if loop in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-understand-this-consecutive-if-loop-embedded-if-loop/m-p/879157#M347331</link>
    <description>&lt;P&gt;Don't write code like that. Just because you can, does not mean you should. When I worked as a SAS developer, I wrote macros that were thousands of lines long and were callable by other macros that were even longer. There were many macro statements, macro functions, and macro variables.&amp;nbsp; Still, my first rule of macro writing was NEVER use the macro language where you don't have to, and equivalently, ALWAYS use ordinary SAS instead when you can. In a DATA step, that means access the macro variables when you can via SYMGET, process their values in ordinary DATA step variables by using ordinary DATA step statements and functions, and if necessary, send the results into macro variables via SYMPUTX.&lt;/P&gt;</description>
    <pubDate>Sat, 03 Jun 2023 13:22:19 GMT</pubDate>
    <dc:creator>WarrenKuhfeld</dc:creator>
    <dc:date>2023-06-03T13:22:19Z</dc:date>
    <item>
      <title>How to understand this consecutive if loop embedded %if loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-understand-this-consecutive-if-loop-embedded-if-loop/m-p/879153#M347328</link>
      <description>&lt;P&gt;The program is:&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;%macro loop(testcd=);
data data2;
    set data;
	if %scan(&amp;amp;testcd.,1) eq 1 %if %length(&amp;amp;testcd.) gt %length(%scan(&amp;amp;testcd.,1)) %then %do; and %scan(&amp;amp;testcd.,2) eq 1 %end; then do;
	    value=1;
	end;
	else if %scan(&amp;amp;testcd.,1) eq 0 %if %length(&amp;amp;testcd.) gt %length(%scan(&amp;amp;testcd.,1)) %then %do; and %scan(&amp;amp;testcd.,2) eq 0 %end; then do;
		value=0;
	end;
run;
%mend;
%loop(testcd=%str(Indx55));&lt;BR /&gt;&lt;BR /&gt;%macro loop2(testcd=);&lt;BR /&gt;data aa;&lt;BR /&gt;set data;&lt;BR /&gt;a=%scan(&amp;amp;testcd.,1);&lt;BR /&gt;b=%length(%scan(&amp;amp;testcd.,1));&lt;BR /&gt;run;&lt;BR /&gt;%mend;&lt;BR /&gt;%loop2(testcd=%str(Indx55));&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The program creates a new column VALUE, my question is that what does the if loop mean? I only know the form like&amp;nbsp;&lt;/P&gt;&lt;P&gt;if xx then do; xxx; end;&lt;/P&gt;&lt;P&gt;or&amp;nbsp;&lt;/P&gt;&lt;P&gt;%if xx %then %do; xxx; %end;&lt;/P&gt;&lt;P&gt;But it is&lt;/P&gt;&lt;P&gt;if xx %if xx %then %do; and xxx %end; then do; end;&amp;nbsp;&lt;/P&gt;&lt;P&gt;which seems abnormal in syntax. But it did run successfully. Does anyone know how to understand this program ?&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The data and the data2 in the above program are like this,&lt;/P&gt;&lt;P&gt;data -&amp;nbsp;&lt;/P&gt;&lt;P&gt;AVISIT INDX55&lt;/P&gt;&lt;P&gt;Week2 0&lt;/P&gt;&lt;P&gt;Week4 0&lt;/P&gt;&lt;P&gt;Week8 1&lt;/P&gt;&lt;P&gt;Week12 1&lt;/P&gt;&lt;P&gt;...&lt;/P&gt;&lt;P&gt;data2 -&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="a.PNG" style="width: 314px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/84729i33B3E6E437C09998/image-size/large?v=v2&amp;amp;px=999" role="button" title="a.PNG" alt="a.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Another question is that why %scan(&amp;amp;testcd.,1)=the value of Indx55, while&amp;nbsp;%length(%scan(&amp;amp;testcd.,1))=6? (see %loop2 above)&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="a.PNG" style="width: 408px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/84730i8832B34E0CB800F6/image-size/large?v=v2&amp;amp;px=999" role="button" title="a.PNG" alt="a.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 03 Jun 2023 11:19:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-understand-this-consecutive-if-loop-embedded-if-loop/m-p/879153#M347328</guid>
      <dc:creator>Sally_Caffrey</dc:creator>
      <dc:date>2023-06-03T11:19:42Z</dc:date>
    </item>
    <item>
      <title>Re: How to understand this consecutive if loop embedded %if loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-understand-this-consecutive-if-loop-embedded-if-loop/m-p/879154#M347329</link>
      <description>&lt;P&gt;There is no actual loop. And so the macro name is misleading. This code assigns a value of either zero or one to the new variable named VALUE, depending on whether or not certain IF conditions are satisfied. That's all it does is see if the conditions are met. This code seems like a very difficult way to code something that doesn't need to be that difficult.&lt;/P&gt;</description>
      <pubDate>Sat, 03 Jun 2023 11:29:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-understand-this-consecutive-if-loop-embedded-if-loop/m-p/879154#M347329</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-06-03T11:29:08Z</dc:date>
    </item>
    <item>
      <title>Re: How to understand this consecutive if loop embedded %if loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-understand-this-consecutive-if-loop-embedded-if-loop/m-p/879155#M347330</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/433517"&gt;@Sally_Caffrey&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/433517"&gt;@Sally_Caffrey&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I only know the form like&amp;nbsp;&lt;/P&gt;
&lt;P&gt;if xx then do; xxx; end;&lt;/P&gt;
&lt;P&gt;or&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%if xx %then %do; xxx; %end;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;These two structures are sufficient to understand the code if you consider in addition that&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;in a first phase, the &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/mcrolref/p0eksviivbw6bwn1eg6h2crvl7ct.htm" target="_blank" rel="noopener"&gt;macro processor&lt;/A&gt; resolves all macro code (&lt;STRONG&gt;%&lt;/STRONG&gt;scan(), &lt;STRONG&gt;&amp;amp;&lt;/STRONG&gt;testcd., &lt;STRONG&gt;%&lt;/STRONG&gt;length(), &lt;STRONG&gt;%&lt;/STRONG&gt;if ... &lt;STRONG&gt;%&lt;/STRONG&gt;then, &lt;STRONG&gt;%&lt;/STRONG&gt;do/&lt;STRONG&gt;%&lt;/STRONG&gt;end, etc.)&lt;/LI&gt;
&lt;LI&gt;in the second phase, the resulting SAS code is processed (here: the DATA step is compiled and executed).&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;So, with your example macro call&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=""&gt;%loop(testcd=%str(Indx55));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;in the first phase:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;FONT face="courier new,courier"&gt;&amp;amp;testcd.&lt;/FONT&gt; is replaced by &lt;FONT face="courier new,courier"&gt;Indx55&lt;/FONT&gt;&lt;/LI&gt;
&lt;LI&gt;then &lt;FONT face="courier new,courier"&gt;%scan(Indx55,1)&lt;/FONT&gt;&amp;nbsp;is replaced by &lt;FONT face="courier new,courier"&gt;Indx55&lt;/FONT&gt;&amp;nbsp;(the first word in the string "Indx55")&lt;/LI&gt;
&lt;LI&gt;&lt;FONT face="courier new,courier"&gt;%length(Indx55)&lt;/FONT&gt;&amp;nbsp;is replaced by 6&amp;nbsp;(the length of the string "Indx55")&lt;/LI&gt;
&lt;LI&gt;the resulting %IF condition&lt;FONT face="courier new,courier"&gt;&amp;nbsp;6 gt 6&amp;nbsp;&lt;/FONT&gt;evaluates to FALSE, which means that the %DO-%END block following &lt;FONT face="courier new,courier"&gt;%then&lt;/FONT&gt; is not executed, i.e., the SAS code &lt;FONT face="courier new,courier"&gt;and&lt;/FONT&gt; ... &lt;FONT face="courier new,courier"&gt;eq 1&lt;/FONT&gt; is not generated.&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;The resulting SAS code passed to the DATA step compiler for further processing in the second phase is:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data data2;
set data;
if Indx55 eq 1 then do;
  value=1;
end;
else if Indx55 eq 0 then do;
  value=0;
end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;(It could be simplified, e.g., by omitting &lt;FONT face="courier new,courier"&gt;do;&lt;/FONT&gt; and &lt;FONT face="courier new,courier"&gt;end;&lt;/FONT&gt;. Also, an IN condition could be used: &lt;FONT face="courier new,courier"&gt;if Indx55 in (0, 1) then value=Indx55;&lt;/FONT&gt;).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Similarly, with a macro call like&lt;/P&gt;
&lt;PRE&gt;%loop(testcd=%str(Indx55,Indx66));&lt;/PRE&gt;
&lt;P&gt;the %IF condition &lt;EM&gt;is&lt;/EM&gt; met (&lt;FONT face="courier new,courier"&gt;13 gt 6&lt;/FONT&gt;) and the resulting SAS code looks like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data data2;
set data;
if Indx55 eq 1 and Indx66 eq 1 then do;
  value=1;
end;
else if Indx55 eq 0 and Indx66 eq 0 then do;
  value=0;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In the DATA step code generated by macro %LOOP2, called as &lt;FONT face="courier new,courier"&gt;%loop2(testcd=%str(Indx55));&lt;/FONT&gt;, the statements&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;a=Indx55;
b=6;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;are interpreted as usual: variable &lt;FONT face="courier new,courier"&gt;a&lt;/FONT&gt; is assigned the value of variable &lt;FONT face="courier new,courier"&gt;Indx55&lt;/FONT&gt; and variable &lt;FONT face="courier new,courier"&gt;b&lt;/FONT&gt; the constant value &lt;FONT face="courier new,courier"&gt;6&lt;/FONT&gt;. Again, the timing -- &lt;EM&gt;first&lt;/EM&gt; resolution of macro code, &lt;EM&gt;then&lt;/EM&gt; compilation of DATA step code -- is important.&lt;/P&gt;</description>
      <pubDate>Sat, 03 Jun 2023 12:59:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-understand-this-consecutive-if-loop-embedded-if-loop/m-p/879155#M347330</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2023-06-03T12:59:51Z</dc:date>
    </item>
    <item>
      <title>Re: How to understand this consecutive if loop embedded %if loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-understand-this-consecutive-if-loop-embedded-if-loop/m-p/879157#M347331</link>
      <description>&lt;P&gt;Don't write code like that. Just because you can, does not mean you should. When I worked as a SAS developer, I wrote macros that were thousands of lines long and were callable by other macros that were even longer. There were many macro statements, macro functions, and macro variables.&amp;nbsp; Still, my first rule of macro writing was NEVER use the macro language where you don't have to, and equivalently, ALWAYS use ordinary SAS instead when you can. In a DATA step, that means access the macro variables when you can via SYMGET, process their values in ordinary DATA step variables by using ordinary DATA step statements and functions, and if necessary, send the results into macro variables via SYMPUTX.&lt;/P&gt;</description>
      <pubDate>Sat, 03 Jun 2023 13:22:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-understand-this-consecutive-if-loop-embedded-if-loop/m-p/879157#M347331</guid>
      <dc:creator>WarrenKuhfeld</dc:creator>
      <dc:date>2023-06-03T13:22:19Z</dc:date>
    </item>
    <item>
      <title>Re: How to understand this consecutive if loop embedded %if loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-understand-this-consecutive-if-loop-embedded-if-loop/m-p/879160#M347334</link>
      <description>Thanks for such detailed explanation FreelanceReinh! Now I am clear how this loop works.&lt;BR /&gt;</description>
      <pubDate>Sat, 03 Jun 2023 14:57:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-understand-this-consecutive-if-loop-embedded-if-loop/m-p/879160#M347334</guid>
      <dc:creator>Sally_Caffrey</dc:creator>
      <dc:date>2023-06-03T14:57:08Z</dc:date>
    </item>
    <item>
      <title>Re: How to understand this consecutive if loop embedded %if loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-understand-this-consecutive-if-loop-embedded-if-loop/m-p/879161#M347335</link>
      <description>Thanks PaigeMiller. Yes I agree with you, this program can be simplified to simple data step.</description>
      <pubDate>Sat, 03 Jun 2023 15:01:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-understand-this-consecutive-if-loop-embedded-if-loop/m-p/879161#M347335</guid>
      <dc:creator>Sally_Caffrey</dc:creator>
      <dc:date>2023-06-03T15:01:29Z</dc:date>
    </item>
    <item>
      <title>Re: How to understand this consecutive if loop embedded %if loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-understand-this-consecutive-if-loop-embedded-if-loop/m-p/879162#M347336</link>
      <description>Thanks for introduction WarrenKuhfeld. I agree SYMGET is better and will try.</description>
      <pubDate>Sat, 03 Jun 2023 15:05:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-understand-this-consecutive-if-loop-embedded-if-loop/m-p/879162#M347336</guid>
      <dc:creator>Sally_Caffrey</dc:creator>
      <dc:date>2023-06-03T15:05:45Z</dc:date>
    </item>
  </channel>
</rss>

