<?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: RETAIN Statement before SET - Very unexpected behaviour! in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/RETAIN-Statement-before-SET-Very-unexpected-behaviour/m-p/967588#M376398</link>
    <description>&lt;P&gt;In this particular case, you do not need to use Dummy on the RETAIN statement. Any new variables created in the DATA step will automatically be added last to the PDV.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data start ;
	var1 = 1 ;
	var2 = 2 ;
	output ;
	var1 = . ;
	var2 = 2 ; 
	output ;
run ;

data end ;
	retain var2 var1;
	set start ;

	if var1 = 1 then dummy = 1 ;
run ;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 28 May 2025 13:32:28 GMT</pubDate>
    <dc:creator>Kathryn_SAS</dc:creator>
    <dc:date>2025-05-28T13:32:28Z</dc:date>
    <item>
      <title>RETAIN Statement before SET - Very unexpected behaviour!</title>
      <link>https://communities.sas.com/t5/SAS-Programming/RETAIN-Statement-before-SET-Very-unexpected-behaviour/m-p/967584#M376395</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I need to control the order of the variables in a given data set and use RETAIN before the SET statement. Here is an example:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;PRE&gt;data start ;
	var1 = 1 ;
	var2 = 2 ;
	output ;
	var1 = . ;
	var2 = 2 ; 
	output ;
run ;

data end ;
	retain var2 var1 dummy ;
	set start ;

	if var1 = 1 then dummy = 1 ;
run ;&lt;/PRE&gt;&lt;P&gt;I have done this dozens of times in the past but now I encountered a problem. The resulting data set looks like that:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;var2&lt;/TD&gt;&lt;TD&gt;var1&lt;/TD&gt;&lt;TD&gt;dummy&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I expected dummy in row 2 to be a missing. To me it seems that dummy is retained although I only used RETAIN to order the columns which should have no effect on the values of the variable itself according to this &lt;A href="https://sas.service-now.com/csm?id=kb_article&amp;amp;sysparm_article=KB0036123" target="_self"&gt;source&lt;/A&gt;. Can anybody explain this to me?&lt;/P&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;</description>
      <pubDate>Wed, 28 May 2025 13:13:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/RETAIN-Statement-before-SET-Very-unexpected-behaviour/m-p/967584#M376395</guid>
      <dc:creator>sas_nutzer</dc:creator>
      <dc:date>2025-05-28T13:13:30Z</dc:date>
    </item>
    <item>
      <title>Re: RETAIN Statement before SET - Very unexpected behaviour!</title>
      <link>https://communities.sas.com/t5/SAS-Programming/RETAIN-Statement-before-SET-Very-unexpected-behaviour/m-p/967586#M376396</link>
      <description>&lt;P&gt;You attempted to use a SIDE EFFECT of the RETAIN statement.&amp;nbsp; You cannot get the side effect without the statement also doing what it was intended to do.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Use a different method to change the variable order.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data end ;
  if 0 then set start(keep=var2) start(keep=var1);
  length dummy 8;
  set start ;
  if var1 = 1 then dummy = 1 ;
run ;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 28 May 2025 13:23:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/RETAIN-Statement-before-SET-Very-unexpected-behaviour/m-p/967586#M376396</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2025-05-28T13:23:30Z</dc:date>
    </item>
    <item>
      <title>Re: RETAIN Statement before SET - Very unexpected behaviour!</title>
      <link>https://communities.sas.com/t5/SAS-Programming/RETAIN-Statement-before-SET-Very-unexpected-behaviour/m-p/967587#M376397</link>
      <description>&lt;P&gt;According to my understanding of RETAIN, the code is doing what it is supposed to do. Since DUMMY is a retained variable, and for the second row where var1=. DUMMY does not get assigned a value in the code. So it RETAINs the value from the previous observation.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Over the years, I have seen a lot of people spend a lot of time trying to get columns in a specific order. And I think in 99.9% of the cases, there is no need to do this at all, and the time spent ordering the variables is essentially not productive in my mind. Maybe yours is the 0.1% of the time where a specific order is required — please explain why you think you must have a specific order.&lt;/P&gt;</description>
      <pubDate>Wed, 28 May 2025 13:24:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/RETAIN-Statement-before-SET-Very-unexpected-behaviour/m-p/967587#M376397</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2025-05-28T13:24:36Z</dc:date>
    </item>
    <item>
      <title>Re: RETAIN Statement before SET - Very unexpected behaviour!</title>
      <link>https://communities.sas.com/t5/SAS-Programming/RETAIN-Statement-before-SET-Very-unexpected-behaviour/m-p/967588#M376398</link>
      <description>&lt;P&gt;In this particular case, you do not need to use Dummy on the RETAIN statement. Any new variables created in the DATA step will automatically be added last to the PDV.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data start ;
	var1 = 1 ;
	var2 = 2 ;
	output ;
	var1 = . ;
	var2 = 2 ; 
	output ;
run ;

data end ;
	retain var2 var1;
	set start ;

	if var1 = 1 then dummy = 1 ;
run ;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 28 May 2025 13:32:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/RETAIN-Statement-before-SET-Very-unexpected-behaviour/m-p/967588#M376398</guid>
      <dc:creator>Kathryn_SAS</dc:creator>
      <dc:date>2025-05-28T13:32:28Z</dc:date>
    </item>
    <item>
      <title>Re: RETAIN Statement before SET - Very unexpected behaviour!</title>
      <link>https://communities.sas.com/t5/SAS-Programming/RETAIN-Statement-before-SET-Very-unexpected-behaviour/m-p/967589#M376399</link>
      <description>&lt;P&gt;Thank you for the answer, but the &lt;A href="https://sas.service-now.com/csm?id=kb_article&amp;amp;sysparm_article=KB0036123" target="_self"&gt;source&lt;/A&gt; says: "&lt;SPAN&gt;The RETAIN statement is most often used to reorder variables because no other variable attribute specifications are required. The RETAIN statement has no effect on retaining values of existing variables being read from the data set."&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;So it shouldn't behave like that, should it?&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 28 May 2025 13:36:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/RETAIN-Statement-before-SET-Very-unexpected-behaviour/m-p/967589#M376399</guid>
      <dc:creator>sas_nutzer</dc:creator>
      <dc:date>2025-05-28T13:36:02Z</dc:date>
    </item>
    <item>
      <title>Re: RETAIN Statement before SET - Very unexpected behaviour!</title>
      <link>https://communities.sas.com/t5/SAS-Programming/RETAIN-Statement-before-SET-Very-unexpected-behaviour/m-p/967590#M376400</link>
      <description>In this case I need the correct order.</description>
      <pubDate>Wed, 28 May 2025 13:36:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/RETAIN-Statement-before-SET-Very-unexpected-behaviour/m-p/967590#M376400</guid>
      <dc:creator>sas_nutzer</dc:creator>
      <dc:date>2025-05-28T13:36:23Z</dc:date>
    </item>
    <item>
      <title>Re: RETAIN Statement before SET - Very unexpected behaviour!</title>
      <link>https://communities.sas.com/t5/SAS-Programming/RETAIN-Statement-before-SET-Very-unexpected-behaviour/m-p/967591#M376401</link>
      <description>Thank you. This was an artifical example. In the real world case I need the ordering to work.</description>
      <pubDate>Wed, 28 May 2025 13:36:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/RETAIN-Statement-before-SET-Very-unexpected-behaviour/m-p/967591#M376401</guid>
      <dc:creator>sas_nutzer</dc:creator>
      <dc:date>2025-05-28T13:36:48Z</dc:date>
    </item>
    <item>
      <title>Re: RETAIN Statement before SET - Very unexpected behaviour!</title>
      <link>https://communities.sas.com/t5/SAS-Programming/RETAIN-Statement-before-SET-Very-unexpected-behaviour/m-p/967593#M376402</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/382559"&gt;@sas_nutzer&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thank you for the answer, but the &lt;A href="https://sas.service-now.com/csm?id=kb_article&amp;amp;sysparm_article=KB0036123" target="_self"&gt;source&lt;/A&gt; says: "&lt;SPAN&gt;The RETAIN statement is most often used to reorder variables because no other variable attribute specifications are required. The RETAIN statement has no effect on retaining values of existing variables being read from the data set."&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;So it shouldn't behave like that, should it?&lt;/SPAN&gt;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;The important part is this:&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;STRONG&gt;&lt;SPAN&gt;of existing variables being read from the data set&lt;/SPAN&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Since dummy is not being read from a dataset, it is affected by the RETAIN statement.&lt;/P&gt;
&lt;P&gt;Although existing variables (those read by a SET or MERGE) are retained by definition, they are always overwritten when a new observation is read. Dummy is not overwritten and therefore keeps its value.&lt;/P&gt;</description>
      <pubDate>Wed, 28 May 2025 13:43:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/RETAIN-Statement-before-SET-Very-unexpected-behaviour/m-p/967593#M376402</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2025-05-28T13:43:40Z</dc:date>
    </item>
    <item>
      <title>Re: RETAIN Statement before SET - Very unexpected behaviour!</title>
      <link>https://communities.sas.com/t5/SAS-Programming/RETAIN-Statement-before-SET-Very-unexpected-behaviour/m-p/967599#M376403</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/382559"&gt;@sas_nutzer&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;In this case I need the correct order.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Why? What bad things happen if your ordering is not correct? Please explain in detail.&lt;/P&gt;</description>
      <pubDate>Wed, 28 May 2025 13:57:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/RETAIN-Statement-before-SET-Very-unexpected-behaviour/m-p/967599#M376403</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2025-05-28T13:57:12Z</dc:date>
    </item>
    <item>
      <title>Re: RETAIN Statement before SET - Very unexpected behaviour!</title>
      <link>https://communities.sas.com/t5/SAS-Programming/RETAIN-Statement-before-SET-Very-unexpected-behaviour/m-p/967607#M376404</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/382559"&gt;@sas_nutzer&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;In this case I need the correct order.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Why? What bad things happen if your ordering is not correct? Please explain in detail.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;The dataset is difficult for humans to work with.&amp;nbsp; You cannot LOOK at the data without having to rearrange the columns.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Example I have seen way too many times.&amp;nbsp; Someone lazily create a bunch of optional long character variables at the front of the program data vector.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data class;
  length comment1-comment4 $200;
  set sashelp.class;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then when you try to examine the data the whole first page of data is filled with empty columns.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Tom_0-1748442401016.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/107389i74EA0FE91ABBC579/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Tom_0-1748442401016.png" alt="Tom_0-1748442401016.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 28 May 2025 14:26:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/RETAIN-Statement-before-SET-Very-unexpected-behaviour/m-p/967607#M376404</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2025-05-28T14:26:58Z</dc:date>
    </item>
  </channel>
</rss>

