<?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 statements occasionally partially ignored in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/if-statements-occasionally-partially-ignored/m-p/945485#M370416</link>
    <description>&lt;P&gt;Thanks! I used retain to order the variables.&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 27 Sep 2024 17:13:44 GMT</pubDate>
    <dc:creator>help_me_i_dumb</dc:creator>
    <dc:date>2024-09-27T17:13:44Z</dc:date>
    <item>
      <title>if statements occasionally partially ignored</title>
      <link>https://communities.sas.com/t5/SAS-Programming/if-statements-occasionally-partially-ignored/m-p/945386#M370400</link>
      <description>&lt;P&gt;Could anyone tell me why this sometimes happens if I do not explicitly include an else statement? I was expecting b321 to only equal 1 for nAtBat=321. Most of the time I have no problems with this, but once in awhile I spend half the day troubleshooting and am usually so fed up that I just add all the else statements and say good riddance. But I would like to understand why this happens.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;To be clear, nAtBat is the 2nd field. I appreciate any input!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data bb;
    retain b321 nAtBat;
	set sashelp.baseball;
	if nAtBat=321 then b321=1;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&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="Screenshot 2024-09-26 150310.png" style="width: 260px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/100700iE68CAC75910A2A7C/image-dimensions/260x269?v=v2" width="260" height="269" role="button" title="Screenshot 2024-09-26 150310.png" alt="Screenshot 2024-09-26 150310.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 26 Sep 2024 22:29:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/if-statements-occasionally-partially-ignored/m-p/945386#M370400</guid>
      <dc:creator>help_me_i_dumb</dc:creator>
      <dc:date>2024-09-26T22:29:13Z</dc:date>
    </item>
    <item>
      <title>Re: if statements occasionally partially ignored</title>
      <link>https://communities.sas.com/t5/SAS-Programming/if-statements-occasionally-partially-ignored/m-p/945395#M370402</link>
      <description>&lt;P&gt;Hi&lt;BR /&gt;You are using &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lestmtsref/p0t2ac0tfzcgbjn112mu96hkgg9o.htm" target="_self"&gt;RETAIN&lt;/A&gt; statement. &lt;BR /&gt;When the condition is met (&lt;CODE class=""&gt;nAtBat&lt;/CODE&gt;=321) b321 is set to 1 and RETAIN statement then retaining 1 as an initial value of b321 at each iteration of data step. Without RETAIN statement, b321 would be missing (.) where the condition is not met.&lt;BR /&gt;If you need b321=1 only when&amp;nbsp;&lt;CODE class=""&gt;nAtBat&lt;/CODE&gt;=321, then just remove RETAIN statement.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=""&gt;data bb;
  set sashelp.baseball;
  if nAtBat=321 then b321=1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 26 Sep 2024 23:34:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/if-statements-occasionally-partially-ignored/m-p/945395#M370402</guid>
      <dc:creator>A_Kh</dc:creator>
      <dc:date>2024-09-26T23:34:10Z</dc:date>
    </item>
    <item>
      <title>Re: if statements occasionally partially ignored</title>
      <link>https://communities.sas.com/t5/SAS-Programming/if-statements-occasionally-partially-ignored/m-p/945423#M370404</link>
      <description>&lt;P&gt;Why did you use a RETAIN statement for B321 if you did not want the values retained?&lt;/P&gt;
&lt;P&gt;Why did you use a RETAIN statement for NATBAT when it already exists in the dataset SASHELP.BASEBALL?&amp;nbsp; Variables that are sources from existing datasets are always already retained (that is why one to many merges work properly)&amp;nbsp; In your data step the retained value is never used since you immediately read a new value from the dataset when SET statement executes.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want those two variables to be the first two then why not just use a LENGTH statement instead?&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  length b321&amp;nbsp;natbat&amp;nbsp;8;
&amp;nbsp;&amp;nbsp;set&amp;nbsp;sashelp.baseball;
...&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 27 Sep 2024 02:00:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/if-statements-occasionally-partially-ignored/m-p/945423#M370404</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-09-27T02:00:01Z</dc:date>
    </item>
    <item>
      <title>Re: if statements occasionally partially ignored</title>
      <link>https://communities.sas.com/t5/SAS-Programming/if-statements-occasionally-partially-ignored/m-p/945485#M370416</link>
      <description>&lt;P&gt;Thanks! I used retain to order the variables.&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 27 Sep 2024 17:13:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/if-statements-occasionally-partially-ignored/m-p/945485#M370416</guid>
      <dc:creator>help_me_i_dumb</dc:creator>
      <dc:date>2024-09-27T17:13:44Z</dc:date>
    </item>
    <item>
      <title>Re: if statements occasionally partially ignored</title>
      <link>https://communities.sas.com/t5/SAS-Programming/if-statements-occasionally-partially-ignored/m-p/945487#M370418</link>
      <description>"Why did you use a RETAIN statement for B321 if you did not want the values retained? Why did you use a RETAIN statement for NATBAT when it already exists in the dataset SASHELP.BASEBALL?"&lt;BR /&gt;&lt;BR /&gt;Because I'm ignorant. That doesn't entitle you to be condescending.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Fri, 27 Sep 2024 17:40:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/if-statements-occasionally-partially-ignored/m-p/945487#M370418</guid>
      <dc:creator>help_me_i_dumb</dc:creator>
      <dc:date>2024-09-27T17:40:34Z</dc:date>
    </item>
    <item>
      <title>Re: if statements occasionally partially ignored</title>
      <link>https://communities.sas.com/t5/SAS-Programming/if-statements-occasionally-partially-ignored/m-p/945492#M370420</link>
      <description>&lt;P&gt;It was a serious question.&amp;nbsp; I think you answered it one of your later posts where you mentioned using it to set the order of the variables in the dataset.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A number of people will use the RETAIN statement for that purpose, but you need to remember that the actual purpose of the RETAIN statement still applied.&amp;nbsp; So it is best to do that in a step that does nothing else (or at least does nothing conditionally).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The reason the RETAIN statement has been found useful in that way is related to how SAS builds up the dataset (what some places in the documentation call the "program data vector") as it first encounters the variable names.&amp;nbsp; The advantage it has over using a LENGTH statement (or many others that would also introduce the variable name) is that it just set the order, but does NOT at that point force the data step compiler to decide the type and storage length of the variables.&amp;nbsp; So you can use with out knowing if the variable is numeric or character (and for character how many bytes of storage it needs).&amp;nbsp; Then when later the data step compiler sees the variable, for example when examining a dataset being read in , it will fix the variable type and length.&lt;/P&gt;</description>
      <pubDate>Fri, 27 Sep 2024 18:30:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/if-statements-occasionally-partially-ignored/m-p/945492#M370420</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-09-27T18:30:09Z</dc:date>
    </item>
  </channel>
</rss>

