<?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: Macro %do loop logic in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Macro-do-loop-logic/m-p/838102#M331418</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/85809"&gt;@fcolina&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;I don't know what happened to the indents I had, they vanished when I pasted to the website.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Use the Insert Code or Insert SAS Code button on the editor menu to get a pop-up window where you can paste text that preserve the spacing.&amp;nbsp; If you put the code into the body of your message it assumes it is paragraphs to be reflowed to fit the current browsers width.&lt;/P&gt;</description>
    <pubDate>Wed, 12 Oct 2022 16:26:27 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2022-10-12T16:26:27Z</dc:date>
    <item>
      <title>Macro %do loop logic</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-do-loop-logic/m-p/838072#M331402</link>
      <description>&lt;P&gt;I am stumped with a very simple do loop problem.&amp;nbsp; For reasons unknown, the loop logic is ignoring the equals sign I'm using so that the output is not what I need.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This works:&lt;/P&gt;&lt;P&gt;data work.retail;&lt;BR /&gt;input year:32. quarter:32. sales:32.;&lt;BR /&gt;datalines;&lt;BR /&gt;2007 1 921266&lt;BR /&gt;2007 2 1013371&lt;BR /&gt;2007 3 1000151&lt;BR /&gt;2007 4 1060394&lt;BR /&gt;2008 1 950268&lt;BR /&gt;2008 2 1028016&lt;BR /&gt;2008 3 999824&lt;BR /&gt;2008 4 957207&lt;BR /&gt;;;;;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;data work.retail2;&lt;BR /&gt;set work.retail;&lt;BR /&gt;if quarter = 1&lt;BR /&gt;then do;&lt;BR /&gt;d1 = 1;&lt;BR /&gt;end;&lt;BR /&gt;else do;&lt;BR /&gt;d1 = 0;&lt;BR /&gt;end;&lt;BR /&gt;if quarter = 2&lt;BR /&gt;then do;&lt;BR /&gt;d2 = 1;&lt;BR /&gt;end;&lt;BR /&gt;else do;&lt;BR /&gt;d2 = 0;&lt;BR /&gt;end;&lt;BR /&gt;if quarter = 3&lt;BR /&gt;then do;&lt;BR /&gt;d3 = 1;&lt;BR /&gt;end;&lt;BR /&gt;else do;&lt;BR /&gt;d3 = 0;&lt;BR /&gt;end;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;/* But this does not work */&lt;/P&gt;&lt;P&gt;%macro MakeRetail2;&lt;BR /&gt;data work.retail2;&lt;BR /&gt;set work.retail;&lt;BR /&gt;%do i =1 %to 3;&lt;BR /&gt;%if quarter = &amp;amp;i&lt;BR /&gt;%then %do;&lt;BR /&gt;d&amp;amp;i = 1;&lt;BR /&gt;%end;&lt;BR /&gt;%else %do;&lt;BR /&gt;d&amp;amp;i = 0;&lt;BR /&gt;%end; * end else-do;&lt;BR /&gt;%end; * end do-loop *;&lt;BR /&gt;output;&lt;BR /&gt;run;&lt;BR /&gt;%mend MakeRetail2;&lt;BR /&gt;&lt;BR /&gt;%MakeRetail2&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;/* Any guidance would be appreciated */&lt;/P&gt;</description>
      <pubDate>Wed, 12 Oct 2022 14:57:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-do-loop-logic/m-p/838072#M331402</guid>
      <dc:creator>fcolina</dc:creator>
      <dc:date>2022-10-12T14:57:28Z</dc:date>
    </item>
    <item>
      <title>Re: Macro %do loop logic</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-do-loop-logic/m-p/838077#M331406</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%if quarter = &amp;amp;i %then %do;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is the problem — %if cannot access the values of data set variables like QUARTER. This actually compares a text string (not the data set variable)&amp;nbsp;&lt;FONT face="courier new,courier"&gt;quarter&lt;/FONT&gt; to the value of &amp;amp;i, and since &amp;amp;i is always an integer, the text string&amp;nbsp;&lt;FONT face="courier new,courier"&gt;quarter&lt;/FONT&gt; is never equal to &amp;amp;i.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So, you can use arrays to eliminate the need for macros. Arrays specifically access the value of data set variables, which macro %IF cannot do. Also the resulting code is much simpler.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data retail2;
    set retail;
    array d d1-d3;
    do i =1 to 3;
        if quarter = i then d(i) = 1; 
        else d(i)=0;
    end;
    drop i;
run;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please also note the indenting of the code makes following the code and following the looping easier, visually.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Lastly, there is rarely a need to create dummy variables like this. What do you plan to do with these dummy variables? Most SAS PROCs don't need dummy variables to do analyses.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 12 Oct 2022 15:12:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-do-loop-logic/m-p/838077#M331406</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-10-12T15:12:43Z</dc:date>
    </item>
    <item>
      <title>Re: Macro %do loop logic</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-do-loop-logic/m-p/838078#M331407</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.retail;
	input year:32. quarter:32. sales:32.;
	datalines;
2007 1 921266
2007 2 1013371
2007 3 1000151
2007 4 1060394
2008 1 950268
2008 2 1028016
2008 3 999824
2008 4 957207
;
	;
	;
	;
run;

data work.retail2_correct;
	set work.retail;

	if quarter=1 then
		do;
			d1=1;
		end;
	else
		do;
			d1=0;
		end;

	if quarter=2 then
		do;
			d2=1;
		end;
	else
		do;
			d2=0;
		end;

	if quarter=3 then
		do;
			d3=1;
		end;
	else
		do;
			d3=0;
		end;
run;

data retail2_array;
	set retail;
	d1=0;
	d2=0;
	d3=0;
	d4=0;
	array d(*) d1-d4;
	d(quarter)=1;
run;

%macro MakeRetail2;
	data work.retail2_macro;
		set work.retail;

		%do i=1 %to 3;

			if quarter=&amp;amp;i
then
				d&amp;amp;i=1;
			else
				d&amp;amp;i=0;
		%end;
		* end else-do;
	run;

%mend MakeRetail2;

%MakeRetail2;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Format your code including indents as a start.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;See the two different options above.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;FYI - see this post plus the links below to see the different options to create dummy variables more easily. And note that if the proc supports a CLASS statement (LOGISTIC/PHREG) this isn't necessarily required at all.&amp;nbsp;&lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-dummy-variables-Categorical-Variables/ta-p/308484" target="_blank"&gt;https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-dummy-variables-Categorical-Variables/ta-p/308484&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 12 Oct 2022 15:09:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-do-loop-logic/m-p/838078#M331407</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2022-10-12T15:09:24Z</dc:date>
    </item>
    <item>
      <title>Re: Macro %do loop logic</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-do-loop-logic/m-p/838084#M331411</link>
      <description>&lt;P&gt;First of all don't bother to use looping when it adds no value.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data retail;
  input year quarter sales;
datalines;
2007 1 921266
2007 2 1013371
2007 3 1000151
2007 4 1060394
2008 1 950268
2008 2 1028016
2008 3 999824
2008 4 957207
;;;;

data retail2;
  set retail;
  d1=quarter=1;
  d2=quarter=2;
  d3=quarter=3;
  d4=quarter=4;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you do need to repeat a series of actions for a set of common variables the default tool to use is an ARRAY.&amp;nbsp; There is no need to use CODE GENERATION.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data retail2;
  set retail;
  array d[4];
  do index=1 to 4;
    d[index]=quarter=index;
  end;
  drop index;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you do have a need for code generation then make sure it is generating the code you want. This macro %DO loop will generate the code in my first data step.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data retail2;
  set retail;
%do quarter=1 %to 4;
  d&amp;amp;quarter=quarter=&amp;amp;quarter;
%end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 12 Oct 2022 15:30:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-do-loop-logic/m-p/838084#M331411</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-10-12T15:30:31Z</dc:date>
    </item>
    <item>
      <title>Re: Macro %do loop logic</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-do-loop-logic/m-p/838097#M331413</link>
      <description>The array approach is far simpler. Thank you for the explanation and thank you to the others that came up with answers for this problem. I really appreciate it.</description>
      <pubDate>Wed, 12 Oct 2022 16:20:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-do-loop-logic/m-p/838097#M331413</guid>
      <dc:creator>fcolina</dc:creator>
      <dc:date>2022-10-12T16:20:22Z</dc:date>
    </item>
    <item>
      <title>Re: Macro %do loop logic</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-do-loop-logic/m-p/838099#M331415</link>
      <description>I don't know what happened to the indents I had, they vanished when I pasted to the website.</description>
      <pubDate>Wed, 12 Oct 2022 16:23:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-do-loop-logic/m-p/838099#M331415</guid>
      <dc:creator>fcolina</dc:creator>
      <dc:date>2022-10-12T16:23:55Z</dc:date>
    </item>
    <item>
      <title>Re: Macro %do loop logic</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-do-loop-logic/m-p/838100#M331416</link>
      <description>Thank you for your reply. The array approach makes more sense than a macro.</description>
      <pubDate>Wed, 12 Oct 2022 16:25:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-do-loop-logic/m-p/838100#M331416</guid>
      <dc:creator>fcolina</dc:creator>
      <dc:date>2022-10-12T16:25:31Z</dc:date>
    </item>
    <item>
      <title>Re: Macro %do loop logic</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-do-loop-logic/m-p/838101#M331417</link>
      <description>Yes thank you, I agree that the array works best.</description>
      <pubDate>Wed, 12 Oct 2022 16:25:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-do-loop-logic/m-p/838101#M331417</guid>
      <dc:creator>fcolina</dc:creator>
      <dc:date>2022-10-12T16:25:55Z</dc:date>
    </item>
    <item>
      <title>Re: Macro %do loop logic</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-do-loop-logic/m-p/838102#M331418</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/85809"&gt;@fcolina&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;I don't know what happened to the indents I had, they vanished when I pasted to the website.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Use the Insert Code or Insert SAS Code button on the editor menu to get a pop-up window where you can paste text that preserve the spacing.&amp;nbsp; If you put the code into the body of your message it assumes it is paragraphs to be reflowed to fit the current browsers width.&lt;/P&gt;</description>
      <pubDate>Wed, 12 Oct 2022 16:26:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-do-loop-logic/m-p/838102#M331418</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-10-12T16:26:27Z</dc:date>
    </item>
    <item>
      <title>Re: Macro %do loop logic</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-do-loop-logic/m-p/838103#M331419</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/85809"&gt;@fcolina&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Yes thank you, I agree that the array works best.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;With an N of 4 the wallpaper code it easier to create and understand then the DO loop.&lt;/P&gt;</description>
      <pubDate>Wed, 12 Oct 2022 16:27:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-do-loop-logic/m-p/838103#M331419</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-10-12T16:27:26Z</dc:date>
    </item>
    <item>
      <title>Re: Macro %do loop logic</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-do-loop-logic/m-p/838190#M331420</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/85809"&gt;@fcolina&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;The array approach is far simpler. Thank you for the explanation and thank you to the others that came up with answers for this problem. I really appreciate it.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/85809"&gt;@fcolina&lt;/a&gt;&amp;nbsp;Next, lets address why you feel you need dummy variables ... this is rarely necessary in most SAS PROCs. Please explain why you need these dummy variables for quarter?&lt;/P&gt;</description>
      <pubDate>Wed, 12 Oct 2022 16:55:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-do-loop-logic/m-p/838190#M331420</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-10-12T16:55:05Z</dc:date>
    </item>
  </channel>
</rss>

