<?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 What happened when by statement is used in data step in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/What-happened-when-by-statement-is-used-in-data-step/m-p/304141#M64707</link>
    <description>&lt;P&gt;data aaa;&lt;BR /&gt; aa = 1; b = 3; output;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;data ccc;&lt;BR /&gt; aa = 2; output;&lt;BR /&gt; aa = 3; output;&lt;BR /&gt; aa = 3; output;&lt;BR /&gt; aa = 3; output;&lt;BR /&gt; aa = 4; output;&lt;BR /&gt; aa = 5; output;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;data aab;&lt;BR /&gt; put _all_;&lt;BR /&gt; set aaa ccc; &lt;BR /&gt;/* by aa;*/&lt;BR /&gt; if aa = 3 then do;&lt;BR /&gt; b = 1;&lt;BR /&gt; b1 = 2;&lt;BR /&gt; end;&lt;BR /&gt; put _all_;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As above, when I comment the by statement, b is retained as 1&amp;nbsp;&amp;nbsp;when aa = 4, 5. However when I uncomment the by statement, the value of b becomes missing. I wonder what happended when by xxx&amp;nbsp;is used with set statement?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;By the way, if&amp;nbsp;the set statement is replaced by merge statement, no matter whether commenting the by statement or not, the value of b never become&amp;nbsp;1 when aa = 4,5&lt;/P&gt;</description>
    <pubDate>Wed, 12 Oct 2016 16:02:53 GMT</pubDate>
    <dc:creator>shengnian</dc:creator>
    <dc:date>2016-10-12T16:02:53Z</dc:date>
    <item>
      <title>What happened when by statement is used in data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/What-happened-when-by-statement-is-used-in-data-step/m-p/304141#M64707</link>
      <description>&lt;P&gt;data aaa;&lt;BR /&gt; aa = 1; b = 3; output;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;data ccc;&lt;BR /&gt; aa = 2; output;&lt;BR /&gt; aa = 3; output;&lt;BR /&gt; aa = 3; output;&lt;BR /&gt; aa = 3; output;&lt;BR /&gt; aa = 4; output;&lt;BR /&gt; aa = 5; output;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;data aab;&lt;BR /&gt; put _all_;&lt;BR /&gt; set aaa ccc; &lt;BR /&gt;/* by aa;*/&lt;BR /&gt; if aa = 3 then do;&lt;BR /&gt; b = 1;&lt;BR /&gt; b1 = 2;&lt;BR /&gt; end;&lt;BR /&gt; put _all_;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As above, when I comment the by statement, b is retained as 1&amp;nbsp;&amp;nbsp;when aa = 4, 5. However when I uncomment the by statement, the value of b becomes missing. I wonder what happended when by xxx&amp;nbsp;is used with set statement?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;By the way, if&amp;nbsp;the set statement is replaced by merge statement, no matter whether commenting the by statement or not, the value of b never become&amp;nbsp;1 when aa = 4,5&lt;/P&gt;</description>
      <pubDate>Wed, 12 Oct 2016 16:02:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/What-happened-when-by-statement-is-used-in-data-step/m-p/304141#M64707</guid>
      <dc:creator>shengnian</dc:creator>
      <dc:date>2016-10-12T16:02:53Z</dc:date>
    </item>
    <item>
      <title>Re: What happened when by statement is used in data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/What-happened-when-by-statement-is-used-in-data-step/m-p/304152#M64713</link>
      <description>&lt;P&gt;You're looking at the effects of a few features.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Variables that come from a SAS data set are automatically retained.&amp;nbsp; That includes B, since it comes from AAA.&amp;nbsp; Without a BY statement, you set B to 1 and nothing replaces B for the rest of the DATA step.&amp;nbsp; So it remains 1 from that point forward. The software has to decide when to set variables to missing when they are brought in from a SAS data set, and does so whenever it switches from one data set to another.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You might be interested to compare that to what happens if you make a slight change to your program:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;if aa=&lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;4&lt;/STRONG&gt;&lt;/FONT&gt; then do;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;With a BY statement, the software has an additional function to perform.&amp;nbsp; Should it ever re-set retained variables to a missing value?&amp;nbsp; The answer depends on whether you use SET or MERGE.&amp;nbsp; With SET + BY, the software re-sets retained variables to missing when it begins reading observations from a new data set.&amp;nbsp; With MERGE + BY, the software re-sets retained variables to missing when it begins a new value of a BY variable.&lt;/P&gt;</description>
      <pubDate>Wed, 12 Oct 2016 17:31:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/What-happened-when-by-statement-is-used-in-data-step/m-p/304152#M64713</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2016-10-12T17:31:46Z</dc:date>
    </item>
    <item>
      <title>Re: What happened when by statement is used in data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/What-happened-when-by-statement-is-used-in-data-step/m-p/304162#M64717</link>
      <description>&lt;P&gt;&lt;SPAN&gt;With SET + BY, the software re-sets retained variables to missing when it begins reading observations from a new data set.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Can you explain more about the&amp;nbsp;new data set? &amp;nbsp;Very grateful.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 12 Oct 2016 18:08:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/What-happened-when-by-statement-is-used-in-data-step/m-p/304162#M64717</guid>
      <dc:creator>shengnian</dc:creator>
      <dc:date>2016-10-12T18:08:56Z</dc:date>
    </item>
    <item>
      <title>Re: What happened when by statement is used in data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/What-happened-when-by-statement-is-used-in-data-step/m-p/304164#M64718</link>
      <description>&lt;P&gt;Consider an abbreviated version of your example:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data combined;&lt;/P&gt;
&lt;P&gt;set aaa ccc;&lt;/P&gt;
&lt;P&gt;by aa;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;AAA contains both AA and B. CCC contains AA only.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As the DATA step processes the observations, it alternates reading observations from AAA and CCC.&amp;nbsp; As part of that process, whenever it switches from one data set to the other, it reinitializes B to missing.&amp;nbsp; &lt;STRONG&gt;After that&lt;/STRONG&gt;, if the next observation comes from AAA, it replaces B.&amp;nbsp; If the next observation comes from CCC, it does not replace B.&lt;/P&gt;</description>
      <pubDate>Wed, 12 Oct 2016 18:46:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/What-happened-when-by-statement-is-used-in-data-step/m-p/304164#M64718</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2016-10-12T18:46:06Z</dc:date>
    </item>
    <item>
      <title>Re: What happened when by statement is used in data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/What-happened-when-by-statement-is-used-in-data-step/m-p/304167#M64719</link>
      <description>&lt;P&gt;&lt;EM&gt;You answered my question perfectly ! Thanks, Astounding.&lt;/EM&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 12 Oct 2016 18:35:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/What-happened-when-by-statement-is-used-in-data-step/m-p/304167#M64719</guid>
      <dc:creator>shengnian</dc:creator>
      <dc:date>2016-10-12T18:35:44Z</dc:date>
    </item>
  </channel>
</rss>

