<?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: Wrong output dataset after using SET and IF-THEN in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Wrong-output-dataset-after-using-SET-and-IF-THEN/m-p/989140#M380165</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4"&gt;@ChrisHemedinger&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;If the DATA step isn't following the logic you expect, I recommend using the DATA step debugger (available in EG and in SAS Studio within Viya) to step through.&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Using the data step debugger is a good idea.&lt;BR /&gt;You can easily follow what's happening !&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;For information about the SAS Studio DATA step debugger, see&amp;nbsp;&lt;/SPAN&gt;&lt;A title="opens in a new browser tab" href="http://documentation.sas.com/?activeCdc=webeditorcdc&amp;amp;cdcId=sasstudiocdc&amp;amp;cdcVersion=5.2&amp;amp;docsetId=webeditorug&amp;amp;docsetTarget=n1hvj6m33v3pidn1f136i22e18br.htm&amp;amp;docsetVersion=5.2"&gt;Using the Data Step Debugger&lt;/A&gt;&lt;SPAN&gt;&amp;nbsp;in&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN class="xisDoc-xrefBookTitle"&gt;SAS Studio: User’s Guide&lt;/SPAN&gt;&lt;SPAN&gt;.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;The original DATA step debugger remains available in the SAS windowing environment (and Enterprise Guide). The documentation for the original DATA step debugger has moved to&amp;nbsp;&lt;A tabindex="0" href="https://go.documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/cmpdebugug/titlepage.htm" target="_blank"&gt;&lt;I&gt;SAS Code Debugger: User’s Guide&lt;/I&gt;&lt;/A&gt;.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;BR, Koen&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 03 Jun 2026 10:06:55 GMT</pubDate>
    <dc:creator>sbxkoenk</dc:creator>
    <dc:date>2026-06-03T10:06:55Z</dc:date>
    <item>
      <title>Wrong output dataset after using SET and IF-THEN</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Wrong-output-dataset-after-using-SET-and-IF-THEN/m-p/989132#M380160</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;when I am using the below code. I get wrong result.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;data a1;&lt;BR /&gt;x='abc';&lt;BR /&gt;run;&lt;BR /&gt;data a2;&lt;BR /&gt;y='y';output;&lt;BR /&gt;y='';output;&lt;BR /&gt;run;&lt;BR /&gt;data a3;&lt;BR /&gt;set a1 a2;&lt;BR /&gt;if y='y' then x='yes';&lt;BR /&gt;run;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Since in the third obs of dataset a3, y= '', I expect to get the result x='', but in fact, the result is x='yes'.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 03 Jun 2026 09:18:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Wrong-output-dataset-after-using-SET-and-IF-THEN/m-p/989132#M380160</guid>
      <dc:creator>aktives</dc:creator>
      <dc:date>2026-06-03T09:18:12Z</dc:date>
    </item>
    <item>
      <title>Re: Wrong output dataset after using SET and IF-THEN</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Wrong-output-dataset-after-using-SET-and-IF-THEN/m-p/989133#M380161</link>
      <description>&lt;P&gt;&lt;STRONG&gt;What happens when the SET statement misbehaves—and how you can fix it!&lt;/STRONG&gt;&lt;BR /&gt;By Kim Wilson on SAS Users February 21, 2014&lt;BR /&gt;&lt;A href="https://blogs.sas.com/content/sgf/2014/02/21/what-happens-when-the-set-statement-misbehaves-and-how-you-can-fix-it/#prettyPhoto" target="_blank"&gt;https://blogs.sas.com/content/sgf/2014/02/21/what-happens-when-the-set-statement-misbehaves-and-how-you-can-fix-it/#prettyPhoto&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;KB0041936&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;Variables read using SET, MERGE, and UPDATE statements are automatically retained&lt;/STRONG&gt;&lt;BR /&gt;&lt;A href="https://sas.service-now.com/csm/en/variables-read-using-set-merge-and-update-statements-are-automatically?id=kb_article_view&amp;amp;sysparm_article=KB0041936" target="_blank"&gt;https://sas.service-now.com/csm/en/variables-read-using-set-merge-and-update-statements-are-automatically?id=kb_article_view&amp;amp;sysparm_article=KB0041936&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You get the results you want - with same number of steps - when you do something like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data a1;
 x='abc';
run;

data a2;
 y='y'; x=''; output;
 y='' ; x=''; output;
run;

data a3;
 set a1 a2;
 if y='y' then x='yes';
run;
/* end of program */&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;BR, Koen&lt;/P&gt;</description>
      <pubDate>Wed, 03 Jun 2026 09:37:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Wrong-output-dataset-after-using-SET-and-IF-THEN/m-p/989133#M380161</guid>
      <dc:creator>sbxkoenk</dc:creator>
      <dc:date>2026-06-03T09:37:46Z</dc:date>
    </item>
    <item>
      <title>Re: Wrong output dataset after using SET and IF-THEN</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Wrong-output-dataset-after-using-SET-and-IF-THEN/m-p/989136#M380162</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I see the expected result. In the second observation the value is "yes" but it's empty in obs 1 and 3.&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="ChrisHemedinger_0-1780479477152.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/115612iF708AAF8FDA2325C/image-size/medium?v=v2&amp;amp;px=400" role="button" title="ChrisHemedinger_0-1780479477152.png" alt="ChrisHemedinger_0-1780479477152.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If the DATA step isn't following the logic you expect, I recommend using the DATA step debugger (available in EG and in SAS Studio within Viya) to step through.&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="ChrisHemedinger_1-1780479596458.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/115613i0E1D3D10FF29DFB3/image-size/medium?v=v2&amp;amp;px=400" role="button" title="ChrisHemedinger_1-1780479596458.png" alt="ChrisHemedinger_1-1780479596458.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 03 Jun 2026 09:40:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Wrong-output-dataset-after-using-SET-and-IF-THEN/m-p/989136#M380162</guid>
      <dc:creator>ChrisHemedinger</dc:creator>
      <dc:date>2026-06-03T09:40:14Z</dc:date>
    </item>
    <item>
      <title>Re: Wrong output dataset after using SET and IF-THEN</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Wrong-output-dataset-after-using-SET-and-IF-THEN/m-p/989137#M380163</link>
      <description>&lt;P&gt;Firstly , I should claim&lt;/P&gt;
&lt;P&gt;the variable X1 is from dataset A1, so&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;x='yes'&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;is retained for the next all obs.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;The key point is all the variables from datasets would be retained .&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;So if you want to get right result , you should reset variable X .&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data a1;
x='abc';
run;
data a2;
y='y';output;
y='';output;
run;
data a3;
set a1 &lt;STRONG&gt;a2(in=ina2);&lt;/STRONG&gt;
&lt;STRONG&gt;if ina2 then call missing(x);&lt;/STRONG&gt;
if y='y' then x='yes';
run;&lt;/PRE&gt;</description>
      <pubDate>Wed, 03 Jun 2026 09:44:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Wrong-output-dataset-after-using-SET-and-IF-THEN/m-p/989137#M380163</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2026-06-03T09:44:33Z</dc:date>
    </item>
    <item>
      <title>Re: Wrong output dataset after using SET and IF-THEN</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Wrong-output-dataset-after-using-SET-and-IF-THEN/m-p/989138#M380164</link>
      <description>&lt;P class="cs95E872D0"&gt;&lt;SPAN class="cs78AB3212"&gt;In short ... to understand why you get the results you get ... you have to&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN class="cs78AB3212"&gt;consider what is happening in the &lt;STRONG&gt;program data vector (PDV)&lt;/STRONG&gt;. &amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class="cs95E872D0"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class="cs95E872D0"&gt;&lt;SPAN class="cs78AB3212"&gt;When using a SET statement, the values are automatically retained until the next observations value is written to the program data vector. &amp;nbsp;The program data vector is created at compile time. &amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class="cs95E872D0"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class="cs95E872D0"&gt;&lt;SPAN class="cs78AB3212"&gt;In your code, the variables X and Y do not exist in both data sets but when the program data vector is built, it sees both variables and marks them to RETAIN because they exist in one of the data set where you SET them together.&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class="cs95E872D0"&gt;&lt;SPAN class="cs78AB3212"&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class="cs95E872D0"&gt;&lt;SPAN class="cs78AB3212"&gt;When you create the X and Y variables on both data sets ... you will get the desired results.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class="cs95E872D0"&gt;&lt;SPAN class="cs78AB3212"&gt;[EDIT]&amp;nbsp;Although I understand that this (making sure X and Y variables exist on both data sets) is an unfriendly solution (for the user). I’m actually more in favor of &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;&amp;nbsp;solution (reset variable X to missing). You find the &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;&amp;nbsp;solution somewhat lower in this topic thread.&lt;BR /&gt;&lt;BR /&gt;BR, Koen&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 03 Jun 2026 10:13:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Wrong-output-dataset-after-using-SET-and-IF-THEN/m-p/989138#M380164</guid>
      <dc:creator>sbxkoenk</dc:creator>
      <dc:date>2026-06-03T10:13:21Z</dc:date>
    </item>
    <item>
      <title>Re: Wrong output dataset after using SET and IF-THEN</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Wrong-output-dataset-after-using-SET-and-IF-THEN/m-p/989140#M380165</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4"&gt;@ChrisHemedinger&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;If the DATA step isn't following the logic you expect, I recommend using the DATA step debugger (available in EG and in SAS Studio within Viya) to step through.&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Using the data step debugger is a good idea.&lt;BR /&gt;You can easily follow what's happening !&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;For information about the SAS Studio DATA step debugger, see&amp;nbsp;&lt;/SPAN&gt;&lt;A title="opens in a new browser tab" href="http://documentation.sas.com/?activeCdc=webeditorcdc&amp;amp;cdcId=sasstudiocdc&amp;amp;cdcVersion=5.2&amp;amp;docsetId=webeditorug&amp;amp;docsetTarget=n1hvj6m33v3pidn1f136i22e18br.htm&amp;amp;docsetVersion=5.2"&gt;Using the Data Step Debugger&lt;/A&gt;&lt;SPAN&gt;&amp;nbsp;in&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN class="xisDoc-xrefBookTitle"&gt;SAS Studio: User’s Guide&lt;/SPAN&gt;&lt;SPAN&gt;.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;The original DATA step debugger remains available in the SAS windowing environment (and Enterprise Guide). The documentation for the original DATA step debugger has moved to&amp;nbsp;&lt;A tabindex="0" href="https://go.documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/cmpdebugug/titlepage.htm" target="_blank"&gt;&lt;I&gt;SAS Code Debugger: User’s Guide&lt;/I&gt;&lt;/A&gt;.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;BR, Koen&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 03 Jun 2026 10:06:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Wrong-output-dataset-after-using-SET-and-IF-THEN/m-p/989140#M380165</guid>
      <dc:creator>sbxkoenk</dc:creator>
      <dc:date>2026-06-03T10:06:55Z</dc:date>
    </item>
    <item>
      <title>Re: Wrong output dataset after using SET and IF-THEN</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Wrong-output-dataset-after-using-SET-and-IF-THEN/m-p/989148#M380167</link>
      <description>&lt;P&gt;Adds some PUT statements to your data step to see what is happening.&lt;/P&gt;
&lt;PRE&gt; 80         data a3;
 81           if 0 then set a1 a2;
 82           put _n_=;
 83           put '1. ' (in1 in2 x y) (=) ;
 84           set a1(in=in1) a2(in=in2);
 85           put '2. ' (in1 in2 x y) (=) ;
 86           if y='y' then x='yes';
 87           put '3. ' (in1 in2 x y) (=) ;
 88         run;
 
 _N_=1
 1. in1=0 in2=0 x=  y= 
 2. in1=1 in2=0 x=abc y= 
 3. in1=1 in2=0 x=abc y= 
 _N_=2
 1. in1=1 in2=0 x=abc y= 
 2. in1=0 in2=1 x=  y=y
 3. in1=0 in2=1 x=yes y=y
 _N_=3
 1. in1=0 in2=1 x=yes y=y
 2. in1=0 in2=1 x=yes y= 
 3. in1=0 in2=1 x=yes y= 
 _N_=4
 1. in1=0 in2=1 x=yes y= 
 NOTE: There were 1 observations read from the data set WORK.A1.
 NOTE: There were 2 observations read from the data set WORK.A2.
 NOTE: The data set WORK.A3 has 3 observations and 2 variables.&lt;/PRE&gt;
&lt;P&gt;So on the first iteration before the SET statement all of the variables are missing.&lt;/P&gt;
&lt;P&gt;Once the SET executes X gets a value and the IN= flag variables are updated.&lt;/P&gt;
&lt;P&gt;Since Y is empty the IF does nothing.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now on the second iteration the values of X and also Y and IN1 and IN2 are remembered ("retained" ).&amp;nbsp; Once the SET executes X is cleared since A1 is no longer contributing values.&amp;nbsp; But then the IF statement is true so&amp;nbsp; X is assigned a new value.&amp;nbsp; Which can now never change since A1 is no longer being read from since its has been exhausted.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What is it that you want to happen instead?&lt;/P&gt;
&lt;P&gt;Do you want X to get set to missing when Y is not 'y' then say that in the code?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if y='y' then x='yes';
else x=' ';
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Do you want X to remember the value from the last observation of A1 when Y is not 'y'?&amp;nbsp; If so then you probably need to introduce a new variable.&lt;/P&gt;
&lt;PRE&gt;  if in1 then oldx=x;
  retain oldx;
  if y='y' then x='yes';
  else x=oldx;&lt;/PRE&gt;</description>
      <pubDate>Wed, 03 Jun 2026 14:09:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Wrong-output-dataset-after-using-SET-and-IF-THEN/m-p/989148#M380167</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2026-06-03T14:09:17Z</dc:date>
    </item>
  </channel>
</rss>

