<?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-statement processing in DATA step in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/IF-statement-processing-in-DATA-step/m-p/928358#M365249</link>
    <description>&lt;P&gt;That is exactly what RETAIN is intended for.&amp;nbsp; Note that your usage of RETAIN to set the variable order is just taking advantage of the fact that SAS sets the order of the variables when they first are "seen" by the compiler. It can be useful since a simple RETAIN statement (without any initial values) will not force SAS to set the TYPE of the variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Since the value is retained it can only change when you explicitly change it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You just need to add an ELSE clause.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;IF close_eff_n &amp;gt; 0 THEN DO;
  close_dte_eff = put(close_eff_n,yymmn.);
END;
else close_dte_eff =' ';&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So that the value is set on every observation.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Alternatively you could change the value of the MISSING option and eliminate the IF statement.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;option missing=' ';
....
close_dte_eff = put(close_eff_n,yymmn.);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Remember to set the missing option back to a period after the data step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;PS Your IF statement is checking for values after 01JAN1960 which is the date that zero represents.&amp;nbsp; Is that really what you meant to do?&amp;nbsp; If you wanted to test for missing why not do that instead?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;IF not missing(close_eff_n) THEN DO;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 14 May 2024 17:55:41 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2024-05-14T17:55:41Z</dc:date>
    <item>
      <title>IF-statement processing in DATA step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/IF-statement-processing-in-DATA-step/m-p/928337#M365243</link>
      <description>&lt;P&gt;&lt;STRONG&gt;**ADDENDUM to original post&lt;/STRONG&gt;: I realized that this issue was being caused by starting with a "RETAIN" statement, which I use to put the variables in the desired order. But I'd still like to leave this question up because I'd appreciate any feedback on:&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;How does a RETAIN statement work? When does it affect the outputs of a command in a DATA step?&lt;/LI&gt;&lt;LI&gt;Does anyone have alternate/preferred strategies for reordering the variables in a dataset?&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;Thanks!&lt;/P&gt;&lt;P&gt;***********************************************************************&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Original post:&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT size="4"&gt;Hello SAS community,&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT size="4"&gt;I'm very confused about how SAS deciphers "IF" Statements in the DATA step.&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT size="4"&gt;In this specific case, I'm working with an account dataset that has some conflicting information about when accounts close, and I am constructing an "effective" close date.&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT size="4"&gt;Earlier in my data step, I used some IF statements to construct my desired close date. The last step is to convert that numeric close date to a string variable in the format YYYYMM. &lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT size="4"&gt;Here's what I tried:&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;DATA WORK.dates_test;
SET WORK.raw_dates;

close_eff_n = acct_close_dte_n;&lt;BR /&gt;IF closed = 1 AND acct_close_dte_n = . THEN DO;&lt;BR /&gt;close_eff_n = maxdate_n;&lt;BR /&gt;END;
&lt;BR /&gt;*(omitting some additional logic used here for parsimony);

IF close_eff_n &amp;gt; 0 THEN DO;
	close_dte_eff = put(close_eff_n,yymmn.);
END;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT size="4"&gt;I had earlier written this last segment as:&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;close_dte_eff = put(close_eff_n,yymmn.);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;FONT size="4"&gt;but this populated the string variable close_dte_eff with a value of "." when close_eff_n was missing, which is why I'm now trying to implement this conditional logic.&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT size="4"&gt;&lt;STRONG&gt;The problem is&lt;/STRONG&gt;: where this condition fails, SAS populates the close_dte_eff field with whatever the last non-failed value was, which is completely incorrect.&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT size="4"&gt;e.g.&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT size="4"&gt;I have:&lt;/FONT&gt;&lt;/P&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;FONT size="4"&gt;&lt;STRONG&gt;close_eff_n&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;&lt;FONT size="4"&gt;01MAR2023&lt;/FONT&gt;&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;FONT size="4"&gt;01APR2023&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;FONT size="4"&gt;.&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;FONT size="4"&gt;.&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;FONT size="4"&gt;01JUL2021&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT size="4"&gt;I want:&lt;/FONT&gt;&lt;/P&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;FONT size="4"&gt;&lt;STRONG&gt;close_eff_n&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;FONT size="4"&gt;&lt;STRONG&gt;close_dte_eff&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;&lt;FONT size="4"&gt;01MAR2023&lt;/FONT&gt;&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;&lt;FONT size="4"&gt;202303&lt;/FONT&gt;&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;FONT size="4"&gt;01APR2023&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;FONT size="4"&gt;202304&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;FONT size="4"&gt;.&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;FONT size="4"&gt;.&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;FONT size="4"&gt;01JUL2021&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;FONT size="4"&gt;202107&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT size="4"&gt;But instead I get:&lt;/FONT&gt;&lt;/P&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;FONT size="4"&gt;&lt;STRONG&gt;close_eff_n&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;FONT size="4"&gt;&lt;STRONG&gt;close_dte_eff&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;&lt;FONT size="4"&gt;01MAR2023&lt;/FONT&gt;&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;&lt;FONT size="4"&gt;202303&lt;/FONT&gt;&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;FONT size="4"&gt;01APR2023&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;FONT size="4"&gt;202304&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;FONT size="4"&gt;.&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;FONT size="4"&gt;202304&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;FONT size="4"&gt;.&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;FONT size="4"&gt;202304&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;FONT size="4"&gt;01JUL2021&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;FONT size="4"&gt;202107&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT size="4"&gt;When I tried to replicate this problem with a simplified dataset, i.e. just taking the final input variables and creating the desired output, I got the result I want, so I suspect it might have something to do with the preceding IF-statements.&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT size="4"&gt;I can think of plenty of workarounds to get this to work as intended, so my question is not so much how to fix this, but&amp;nbsp;&lt;EM&gt;why is this happening&lt;/EM&gt;?&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT size="4"&gt;There's something fundamental about how the "IF-statement" is being processed where rows that fail the "IF" condition are being populated with the value of the last row that met that condition, and I would like to understand when SAS applies this behavior and when it does not. I can see this being a useful feature in some limited cases, but it's generally not what I would want to do when applying conditional logic.&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT size="4"&gt;I had thought that these sort of situations where SAS operates on one row depending on what was in the previous row only happen when there is a "BY" statement, but obviously that's incorrect as there is no "BY" statement in this DATA step.&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT size="4"&gt;I'd really appreciate some explanation as to when actions are applied to rows that do not meet the specified condition in an "IF" statement, and how to control that behavior, so I can make sure that the commands I write are applying to the rows that I expect them to apply to.&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT size="4"&gt;Please let me know if I can provide any other context or information that would be helpful.&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT size="4"&gt;Many thanks,&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT size="4"&gt;Scott&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 14 May 2024 17:27:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/IF-statement-processing-in-DATA-step/m-p/928337#M365243</guid>
      <dc:creator>scwein</dc:creator>
      <dc:date>2024-05-14T17:27:29Z</dc:date>
    </item>
    <item>
      <title>Re: IF-statement processing in DATA step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/IF-statement-processing-in-DATA-step/m-p/928347#M365245</link>
      <description>&lt;P&gt;Can you describe the rules involved for selecting the effective close?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;An example input data set and the expected result would go a way toward a workable solution.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Serious comment: DO NOT MAKE YOUR EFFECTIVE DATE A CHARACTER VALUE. As soon as you try to use the effective date you will find that many things are going to involve turning that character value back into an actual date so start with one.&lt;/P&gt;</description>
      <pubDate>Tue, 14 May 2024 17:28:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/IF-statement-processing-in-DATA-step/m-p/928347#M365245</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2024-05-14T17:28:52Z</dc:date>
    </item>
    <item>
      <title>Re: IF-statement processing in DATA step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/IF-statement-processing-in-DATA-step/m-p/928349#M365246</link>
      <description>You're right, the retain statement causes the problem.  In a simple manner.  Retain tells SAS to let the value sit there and don't reset it just you begin a new observation.  IF THEN is irrelevant, although you capitalize on it by adding an ELSE statement:&lt;BR /&gt;&lt;BR /&gt;else close_dte_eff = " ";</description>
      <pubDate>Tue, 14 May 2024 17:30:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/IF-statement-processing-in-DATA-step/m-p/928349#M365246</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2024-05-14T17:30:41Z</dc:date>
    </item>
    <item>
      <title>Re: IF-statement processing in DATA step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/IF-statement-processing-in-DATA-step/m-p/928358#M365249</link>
      <description>&lt;P&gt;That is exactly what RETAIN is intended for.&amp;nbsp; Note that your usage of RETAIN to set the variable order is just taking advantage of the fact that SAS sets the order of the variables when they first are "seen" by the compiler. It can be useful since a simple RETAIN statement (without any initial values) will not force SAS to set the TYPE of the variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Since the value is retained it can only change when you explicitly change it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You just need to add an ELSE clause.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;IF close_eff_n &amp;gt; 0 THEN DO;
  close_dte_eff = put(close_eff_n,yymmn.);
END;
else close_dte_eff =' ';&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So that the value is set on every observation.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Alternatively you could change the value of the MISSING option and eliminate the IF statement.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;option missing=' ';
....
close_dte_eff = put(close_eff_n,yymmn.);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Remember to set the missing option back to a period after the data step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;PS Your IF statement is checking for values after 01JAN1960 which is the date that zero represents.&amp;nbsp; Is that really what you meant to do?&amp;nbsp; If you wanted to test for missing why not do that instead?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;IF not missing(close_eff_n) THEN DO;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 14 May 2024 17:55:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/IF-statement-processing-in-DATA-step/m-p/928358#M365249</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-05-14T17:55:41Z</dc:date>
    </item>
    <item>
      <title>Re: IF-statement processing in DATA step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/IF-statement-processing-in-DATA-step/m-p/928425#M365280</link>
      <description>&lt;P&gt;I suggest you share with us some representative sample data (your table raw_dates) with all the variables required to derive the effective date, show us the desired result based on this sample data and explain us the logic how to get from have to want.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you provide us with this information then we can certainly help you with the code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please amend below code to share the sample data.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data raw_dates;
  infile datalines dsd dlm=',' truncover;
  input closed acct_close_dte:date9.;
  format acct_close_dte date9.;
  datalines;
1,01MAR2023
1,01APR2023
0,01May2023
1,.
1,01JUL2023
;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 15 May 2024 02:56:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/IF-statement-processing-in-DATA-step/m-p/928425#M365280</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2024-05-15T02:56:16Z</dc:date>
    </item>
    <item>
      <title>Re: IF-statement processing in DATA step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/IF-statement-processing-in-DATA-step/m-p/928647#M365383</link>
      <description>&lt;P&gt;Thanks to all for your responses!&lt;/P&gt;</description>
      <pubDate>Thu, 16 May 2024 12:53:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/IF-statement-processing-in-DATA-step/m-p/928647#M365383</guid>
      <dc:creator>scwein</dc:creator>
      <dc:date>2024-05-16T12:53:33Z</dc:date>
    </item>
  </channel>
</rss>

