<?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 Logic of macros assigning inside datastep (cycle as an example) in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Logic-of-macros-assigning-inside-datastep-cycle-as-an-example/m-p/398365#M96358</link>
    <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Could you please explain the logic of the following program (or if do I understand it correctly).&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
	do i=1 to 10;
		call symputx('Y',i);
			Z=&amp;amp;Y.;
				output;
	end;
run;&lt;BR /&gt;&lt;BR /&gt;proc print noobs; run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;In my understanding when the compiler meets &amp;amp;Y the first time it has to assign the first meaning of i, i.e. "1". Then Z takes a value of Y. Then the cycle will be repeated and Z value should change. But in the output I have Z=10. How could it happen? Does this mean that if macroprocessor views that i should be calculated in a cycle it calculates it?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="1.jpg" style="width: 175px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/15378i3CBE7ED2624D791A/image-size/large?v=v2&amp;amp;px=999" role="button" title="1.jpg" alt="1.jpg" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sun, 24 Sep 2017 08:46:27 GMT</pubDate>
    <dc:creator>DmytroYermak</dc:creator>
    <dc:date>2017-09-24T08:46:27Z</dc:date>
    <item>
      <title>Logic of macros assigning inside datastep (cycle as an example)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Logic-of-macros-assigning-inside-datastep-cycle-as-an-example/m-p/398365#M96358</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Could you please explain the logic of the following program (or if do I understand it correctly).&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
	do i=1 to 10;
		call symputx('Y',i);
			Z=&amp;amp;Y.;
				output;
	end;
run;&lt;BR /&gt;&lt;BR /&gt;proc print noobs; run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;In my understanding when the compiler meets &amp;amp;Y the first time it has to assign the first meaning of i, i.e. "1". Then Z takes a value of Y. Then the cycle will be repeated and Z value should change. But in the output I have Z=10. How could it happen? Does this mean that if macroprocessor views that i should be calculated in a cycle it calculates it?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="1.jpg" style="width: 175px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/15378i3CBE7ED2624D791A/image-size/large?v=v2&amp;amp;px=999" role="button" title="1.jpg" alt="1.jpg" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 24 Sep 2017 08:46:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Logic-of-macros-assigning-inside-datastep-cycle-as-an-example/m-p/398365#M96358</guid>
      <dc:creator>DmytroYermak</dc:creator>
      <dc:date>2017-09-24T08:46:27Z</dc:date>
    </item>
    <item>
      <title>Re: Logic of macros assigning inside datastep (cycle as an example)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Logic-of-macros-assigning-inside-datastep-cycle-as-an-example/m-p/398368#M96360</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/58513"&gt;@DmytroYermak&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;All macro stuff is pre-processing. Macro variable &amp;amp;Y resolves before your data step iterates and though has during iteration a constant value.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For your code to work macro variable &amp;amp;Y must have existed before you've run the data step (i.e. created in the same session where you've been implementing code).&lt;/P&gt;
&lt;P&gt;If you close your session and run the code you've posted out of a brand new session, you'll get an error (because macro variable &amp;amp;Y doesn't exist prior to the data step executing).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now for your example: Use SYMGET() to only retrieve the current value of macro variable &amp;amp;Y during the iteration of the data step.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
  do i=1 to 10;
    call symputx('Y',i);
    z=symget("y");
/*    Z=&amp;amp;Y.;*/
    output;
  end;
run;

proc print noobs;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;N.B: You shouldn''t use a symput()/symget() combination in order to save away values for use in a later iteration of the data step. Use LAG() or RETAINed SAS variables instead.&lt;/P&gt;
&lt;P&gt;When using symput() to create and populate a macro variable for later use in another run group: Code in a way that you call the function only as often as required. Calling the macro processor during a SAS data step impacts negatively on performance so it's something you should only do for good reason.&lt;/P&gt;</description>
      <pubDate>Sun, 24 Sep 2017 09:06:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Logic-of-macros-assigning-inside-datastep-cycle-as-an-example/m-p/398368#M96360</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2017-09-24T09:06:52Z</dc:date>
    </item>
    <item>
      <title>Re: Logic of macros assigning inside datastep (cycle as an example)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Logic-of-macros-assigning-inside-datastep-cycle-as-an-example/m-p/398372#M96362</link>
      <description>&lt;P&gt;Thank you for the code and clarifications!&lt;/P&gt;</description>
      <pubDate>Sun, 24 Sep 2017 09:50:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Logic-of-macros-assigning-inside-datastep-cycle-as-an-example/m-p/398372#M96362</guid>
      <dc:creator>DmytroYermak</dc:creator>
      <dc:date>2017-09-24T09:50:56Z</dc:date>
    </item>
  </channel>
</rss>

