<?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: Can we use RETAIN to reorder columns to the last columns of a dataset in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Can-we-use-RETAIN-to-reorder-columns-to-the-last-columns-of-a/m-p/724131#M224814</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/212695"&gt;@Phil_NZ&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461"&gt;@mkeintz&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you for your explanation&lt;/P&gt;
&lt;P&gt;I saw a lot of discussions using the phrase "if 0 then set(merge)", but now I faced in my discussion.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I myself read &lt;A href="https://sasnrd.com/sas-if-0-then-set-data-step/" target="_self"&gt;the document&lt;/A&gt; but I still do not understand why you use it here. Could you please briefly explain it to me and what does it play in your code here?&lt;/P&gt;
&lt;P&gt;And I do not understand why there are two datasets &lt;STRONG&gt;have&lt;/STRONG&gt; in the code. I am not able to understand the mechanism behind, could you please explain it to me ?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if 0 then set have (keep=gvkey iid) have;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Thank you.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;The purpose of this code is to obligate the compiler of the data step to order variables as you want .&amp;nbsp; It relies on the fact that the compiler will arrange variables in the order they are encountered in the code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So the statement&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if 0 then set have (drop=gvkey iid)  have;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;delays exposure of GVKEY and IID to the compiler until all the other variables in the&amp;nbsp; dataset have already been seen by the compiler.&amp;nbsp; GVKEY and IID will be found in the second HAVE, but not the first.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Similarly&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if 0 then set have (keep=gvkey iid)  have;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;exposes GVKEY and IID to the compiler &lt;EM&gt;&lt;STRONG&gt;prior&lt;/STRONG&gt;&lt;/EM&gt; to all the other variables in HAVE.&amp;nbsp; &amp;nbsp; This is effectively what you were doing when you issued a "RETAIN GVKEY IID" statement prior to your "set have" statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In both of my codes above, it doesn't matter what position those variables had in HAVE.&amp;nbsp; &amp;nbsp;GVKEY and IID could be anywhere - left, right, or middle, but using the above will re-order as I describe.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And again, this is just to guide the compiler in the task of ordering variables. It has no influence on the actual data processing, because the "IF 0" condition is never true, so the "THEN SET ..." clauses are not actually executed.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sat, 06 Mar 2021 08:26:24 GMT</pubDate>
    <dc:creator>mkeintz</dc:creator>
    <dc:date>2021-03-06T08:26:24Z</dc:date>
    <item>
      <title>Can we use RETAIN to reorder columns to the last columns of a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Can-we-use-RETAIN-to-reorder-columns-to-the-last-columns-of-a/m-p/724084#M224785</link>
      <description>&lt;P&gt;Hi SAS Users,&lt;/P&gt;
&lt;P&gt;Normally, we use RETAIN to reorder the columns in a dataset.&lt;/P&gt;
&lt;P&gt;For example, I have a list of column names as below&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;GVKEY IID &lt;STRONG&gt;DATADATE&lt;/STRONG&gt; CIK CONML LOC SIC ISIN SEDOL EXCHG AJEXDI CSHOC CSHTRD CURCDD PRCCD PRCHD PRCLD PRCSTD TPCI TRFD ex_rate &lt;STRONG&gt;gviidkey&lt;/STRONG&gt; prccd_abs prccd_abs_ lagprccd_abs_ lagajexdi lagtrfd raw_return log_raw_return raw_return_abs dollar_vol&lt;BR /&gt;&lt;BR /&gt;/*I highlight DATADATE and gviidkey manually for noticing purpose*/&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Normally if I want to put gviidkey and DATADATE in the first two columns, I did:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	retain gviidkey DATADATE;
	set have;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;However, if I want to put the two columns (&lt;STRONG&gt;GVKEY&lt;/STRONG&gt; and &lt;STRONG&gt;IID&lt;/STRONG&gt;) to the last columns of the dataset, what I should do without typing all column names in retain statement?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Warm regards.&lt;/P&gt;</description>
      <pubDate>Fri, 05 Mar 2021 23:00:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Can-we-use-RETAIN-to-reorder-columns-to-the-last-columns-of-a/m-p/724084#M224785</guid>
      <dc:creator>Phil_NZ</dc:creator>
      <dc:date>2021-03-05T23:00:21Z</dc:date>
    </item>
    <item>
      <title>Re: Can we use RETAIN to reorder columns to the last columns of a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Can-we-use-RETAIN-to-reorder-columns-to-the-last-columns-of-a/m-p/724089#M224788</link>
      <description>&lt;P&gt;You don't need retain;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You could put GVKEY IID at the start by:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  if 0 then set have (keep=gvkey iid) have;
  set have;
  ... remaining code ...
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And similarly put them at the end by&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  if 0 then set have (drop=gvkey iid) have;
  set have;
  ... remaining code ...
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This takes advantage of the fact that the compiler orders variables in the sequence in which they are encountered, as it builds the executable code.&amp;nbsp; But the "IF 0" condition, while being recognized by the compiler, nevertheless will not execute, because zero is never true.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;BTW, you could even drop the second "have" argument in the "if 0 then set" statements.&lt;/P&gt;
&lt;P&gt;Also BTW, you could use "if 0 the MERGE"&amp;nbsp; as well as "if 0 then SET".&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 05 Mar 2021 23:19:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Can-we-use-RETAIN-to-reorder-columns-to-the-last-columns-of-a/m-p/724089#M224788</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2021-03-05T23:19:45Z</dc:date>
    </item>
    <item>
      <title>Re: Can we use RETAIN to reorder columns to the last columns of a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Can-we-use-RETAIN-to-reorder-columns-to-the-last-columns-of-a/m-p/724125#M224809</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461"&gt;@mkeintz&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you for your explanation&lt;/P&gt;
&lt;P&gt;I saw a lot of discussions using the phrase "if 0 then set(merge)", but now I faced in my discussion.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I myself read &lt;A href="https://sasnrd.com/sas-if-0-then-set-data-step/" target="_self"&gt;the document&lt;/A&gt; but I still do not understand why you use it here. Could you please briefly explain it to me and what does it play in your code here?&lt;/P&gt;
&lt;P&gt;And I do not understand why there are two datasets &lt;STRONG&gt;have&lt;/STRONG&gt; in the code. I am not able to understand the mechanism behind, could you please explain it to me ?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if 0 then set have (keep=gvkey iid) have;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Thank you.&lt;/P&gt;</description>
      <pubDate>Sat, 06 Mar 2021 06:54:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Can-we-use-RETAIN-to-reorder-columns-to-the-last-columns-of-a/m-p/724125#M224809</guid>
      <dc:creator>Phil_NZ</dc:creator>
      <dc:date>2021-03-06T06:54:23Z</dc:date>
    </item>
    <item>
      <title>Re: Can we use RETAIN to reorder columns to the last columns of a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Can-we-use-RETAIN-to-reorder-columns-to-the-last-columns-of-a/m-p/724131#M224814</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/212695"&gt;@Phil_NZ&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461"&gt;@mkeintz&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you for your explanation&lt;/P&gt;
&lt;P&gt;I saw a lot of discussions using the phrase "if 0 then set(merge)", but now I faced in my discussion.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I myself read &lt;A href="https://sasnrd.com/sas-if-0-then-set-data-step/" target="_self"&gt;the document&lt;/A&gt; but I still do not understand why you use it here. Could you please briefly explain it to me and what does it play in your code here?&lt;/P&gt;
&lt;P&gt;And I do not understand why there are two datasets &lt;STRONG&gt;have&lt;/STRONG&gt; in the code. I am not able to understand the mechanism behind, could you please explain it to me ?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if 0 then set have (keep=gvkey iid) have;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Thank you.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;The purpose of this code is to obligate the compiler of the data step to order variables as you want .&amp;nbsp; It relies on the fact that the compiler will arrange variables in the order they are encountered in the code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So the statement&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if 0 then set have (drop=gvkey iid)  have;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;delays exposure of GVKEY and IID to the compiler until all the other variables in the&amp;nbsp; dataset have already been seen by the compiler.&amp;nbsp; GVKEY and IID will be found in the second HAVE, but not the first.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Similarly&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if 0 then set have (keep=gvkey iid)  have;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;exposes GVKEY and IID to the compiler &lt;EM&gt;&lt;STRONG&gt;prior&lt;/STRONG&gt;&lt;/EM&gt; to all the other variables in HAVE.&amp;nbsp; &amp;nbsp; This is effectively what you were doing when you issued a "RETAIN GVKEY IID" statement prior to your "set have" statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In both of my codes above, it doesn't matter what position those variables had in HAVE.&amp;nbsp; &amp;nbsp;GVKEY and IID could be anywhere - left, right, or middle, but using the above will re-order as I describe.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And again, this is just to guide the compiler in the task of ordering variables. It has no influence on the actual data processing, because the "IF 0" condition is never true, so the "THEN SET ..." clauses are not actually executed.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 06 Mar 2021 08:26:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Can-we-use-RETAIN-to-reorder-columns-to-the-last-columns-of-a/m-p/724131#M224814</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2021-03-06T08:26:24Z</dc:date>
    </item>
    <item>
      <title>Re: Can we use RETAIN to reorder columns to the last columns of a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Can-we-use-RETAIN-to-reorder-columns-to-the-last-columns-of-a/m-p/724143#M224823</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461"&gt;@mkeintz&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you very much, I understand like that:&lt;/P&gt;
&lt;P&gt;Regarding the code&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if 0 then set have (drop=gvkey iid)  have;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So, SAS will encounter other variables first and assumingly dropping gvkey and iid, then because this logic is wrong, so it adds back these two columns, leading to these two columns will be at the last position.&lt;/P&gt;
&lt;P&gt;I hope that I did not fall into any fallacy.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Many thanks and best regards.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 06 Mar 2021 10:16:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Can-we-use-RETAIN-to-reorder-columns-to-the-last-columns-of-a/m-p/724143#M224823</guid>
      <dc:creator>Phil_NZ</dc:creator>
      <dc:date>2021-03-06T10:16:48Z</dc:date>
    </item>
  </channel>
</rss>

