<?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 with colon : in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Retain-with-colon/m-p/958563#M374115</link>
    <description>Sorry. &lt;BR /&gt;retain  CustID  wealth:  Health:;</description>
    <pubDate>Thu, 06 Feb 2025 16:09:35 GMT</pubDate>
    <dc:creator>Ronein</dc:creator>
    <dc:date>2025-02-06T16:09:35Z</dc:date>
    <item>
      <title>Retain with colon :</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-with-colon/m-p/958502#M374083</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;I want to ask please- why retain statement don't&amp;nbsp; order the columns as&amp;nbsp; wish?&lt;/P&gt;
&lt;P&gt;I am using colon&amp;nbsp; because I want dynamic code.(The dates that each field represnt might be changed in future)&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data have;
Input CustID Health2024 Wealth2024  Health2023 Wealth2023;
cards;
1 100 20 200 15
2 150 30 300 18
;
Run;
Data want;
retain  CustID nr_laks:  nr_laks:;
set have;
Run;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 06 Feb 2025 06:58:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-with-colon/m-p/958502#M374083</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2025-02-06T06:58:28Z</dc:date>
    </item>
    <item>
      <title>Re: Retain with colon :</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-with-colon/m-p/958509#M374086</link>
      <description>So how do you wish?&lt;BR /&gt;Are these columns to be generated later in the data step?&lt;BR /&gt;And how will this be flexible?&lt;BR /&gt;Flexibility can be built using macro variables or arrays.</description>
      <pubDate>Thu, 06 Feb 2025 08:27:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-with-colon/m-p/958509#M374086</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2025-02-06T08:27:19Z</dc:date>
    </item>
    <item>
      <title>Re: Retain with colon :</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-with-colon/m-p/958512#M374088</link>
      <description>&lt;P&gt;You want this ?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data have;
Input CustID Health2024 Wealth2024  Health2023 Wealth2023;
cards;
1 100 20 200 15
2 150 30 300 18
;
Run;
Data want;
if 0 then set have(keep=CustID);
if 0 then set have(keep=Wealth:);
if 0 then set have(keep=Health:);
set have;
Run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 06 Feb 2025 08:44:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-with-colon/m-p/958512#M374088</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2025-02-06T08:44:25Z</dc:date>
    </item>
    <item>
      <title>Re: Retain with colon :</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-with-colon/m-p/958522#M374090</link>
      <description>&lt;P&gt;Hi Ronein,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As indicated by others, it is not entirely clear what you want, even though you have specified a &lt;FONT face="courier new,courier"&gt;want&lt;/FONT&gt; data set, but I think you're telling us that there is a problem with that data set.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You have specified &lt;FONT face="courier new,courier"&gt;nr_laks:&lt;/FONT&gt; in your retain statement twice, I expect you only meant this to appear once, but even so, as no such variables starting &lt;FONT face="courier new,courier"&gt;nr_laks&lt;/FONT&gt; appear in your data steps then what do you expect to be retained?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Are you just trying to rearrange existing columns? If yes, then is what you want demonstrated by the below &lt;FONT face="courier new,courier"&gt;want2&lt;/FONT&gt; data set (makes &lt;FONT face="courier new, courier"&gt;sex&amp;nbsp;&lt;/FONT&gt;the last column)? The &lt;FONT face="courier new,courier"&gt;have&lt;/FONT&gt; data set is setting up the &lt;FONT face="courier new,courier"&gt;measure:&lt;/FONT&gt; variables. The &lt;FONT face="courier new,courier"&gt;want&lt;/FONT&gt; data set is, I believe what you might have been trying. If the below is not what you want then provide a &lt;FONT face="courier new,courier"&gt;want&lt;/FONT&gt; data set that demonstrates what output you do want, please.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  set sashelp.class(rename = (age = measure1 weight = measure2 height = measure3));
run;


data want;
  retain name measure:;
  set have;
run;


data want2;
  set have(keep = name measure:);
  set have(keep = sex);
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;Thanks &amp;amp; kind regards,&lt;/P&gt;
&lt;P&gt;Amir.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Edit: corrected variable name.&lt;/P&gt;</description>
      <pubDate>Thu, 06 Feb 2025 10:38:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-with-colon/m-p/958522#M374090</guid>
      <dc:creator>Amir</dc:creator>
      <dc:date>2025-02-06T10:38:59Z</dc:date>
    </item>
    <item>
      <title>Re: Retain with colon :</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-with-colon/m-p/958530#M374095</link>
      <description>&lt;P&gt;Since no variables with names starting with nr_laks are present in your DATA step, and custid is automatically retained (because it is present in an incoming dataset), the RETAIN statement is completely irrelevant and does nothing.&lt;/P&gt;</description>
      <pubDate>Thu, 06 Feb 2025 13:26:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-with-colon/m-p/958530#M374095</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2025-02-06T13:26:29Z</dc:date>
    </item>
    <item>
      <title>Re: Retain with colon :</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-with-colon/m-p/958531#M374096</link>
      <description>&lt;P&gt;Transpose to a long dataset, so data (the years) is not hidden in structure. Maxim 19!!&lt;/P&gt;
&lt;P&gt;As soon as data &lt;U&gt;is&lt;/U&gt; data, no dynamic code is needed, as the data drives the process.&lt;/P&gt;</description>
      <pubDate>Thu, 06 Feb 2025 13:28:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-with-colon/m-p/958531#M374096</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2025-02-06T13:28:57Z</dc:date>
    </item>
    <item>
      <title>Re: Retain with colon :</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-with-colon/m-p/958534#M374097</link>
      <description>&lt;P&gt;I assume you have your RETAIN statement reference health: and wealth: .&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So I think your question is why doesn't:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want1;
  retain CustID health:  wealth:;
  set have;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;work to reorder variables.&lt;BR /&gt;&lt;BR /&gt;While:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want2;
  retain CustID Health2024 Health2023 Wealth2024 Wealth2023;
  set have;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;does work to reorder variables.&lt;BR /&gt;&lt;BR /&gt;I believe the answer is that using the : modifier on a variable name you can refer to *existing* variables, but you cannot *create* a new variable.&lt;BR /&gt;&lt;BR /&gt;When you use the RETAIN statement before the SET statement to reorder variables, it works because the RETAIN statements works at compile time and it creates variables in the PDV, before the variables are created by the SET statement.&lt;BR /&gt;&lt;BR /&gt;But since&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  retain CustID health:  wealth:;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Cannot create variables&amp;nbsp;Health2024 Wealth2024 Health2023 Wealth2023 etc.&amp;nbsp; It's equivalent to:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;retain CustID ;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 06 Feb 2025 13:48:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-with-colon/m-p/958534#M374097</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2025-02-06T13:48:01Z</dc:date>
    </item>
    <item>
      <title>Re: Retain with colon :</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-with-colon/m-p/958542#M374099</link>
      <description>&lt;P&gt;Just try to mentally compile the SAS code yourself so you can see why what you want is impossible.&lt;/P&gt;
&lt;P&gt;1) DATA WANT;&lt;/P&gt;
&lt;P&gt;So you are telling SAS what datasets will be created by this data step.&lt;/P&gt;
&lt;P&gt;2)&amp;nbsp;retain CustID nr_laks: nr_laks:;&lt;/P&gt;
&lt;P&gt;So you passed one actual variable and variable lists to the RETAIN statement.&amp;nbsp; At this point there are no variables yet defined that start with NR_LAKS so you effectively ran this statement:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;retain CustID ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So CUSTID will be retained (which is NOT needed as variables coming from input datasets are already retained) and as a SIDE EFFECT it will force the compiler to add CUSTID into the list of variables for this data step.&lt;/P&gt;
&lt;P&gt;3) SET HAVE;&lt;/P&gt;
&lt;P&gt;So this will cause the compiler to add all of the variables in HAVE to the list of variables for this data step.&amp;nbsp; They will all be retained.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Try this instead:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have(obs=0 keep=CustID)
      have(obs=0 keep=nr_laks:)
      have
  ;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 06 Feb 2025 14:33:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-with-colon/m-p/958542#M374099</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2025-02-06T14:33:17Z</dc:date>
    </item>
    <item>
      <title>Re: Retain with colon :</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-with-colon/m-p/958547#M374104</link>
      <description>&lt;P&gt;Thanks&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Whenever I get a notification that you've answered a question I already answered, I try to read it to see how I could have answered better.&amp;nbsp; : )&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;P&gt;Not sure why I called the colon in health: a "modifier on a variable name," when of course health: is a variable list.&amp;nbsp; And in this case, a variable list with 0 variables in it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 06 Feb 2025 15:00:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-with-colon/m-p/958547#M374104</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2025-02-06T15:00:51Z</dc:date>
    </item>
    <item>
      <title>Re: Retain with colon :</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-with-colon/m-p/958563#M374115</link>
      <description>Sorry. &lt;BR /&gt;retain  CustID  wealth:  Health:;</description>
      <pubDate>Thu, 06 Feb 2025 16:09:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-with-colon/m-p/958563#M374115</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2025-02-06T16:09:35Z</dc:date>
    </item>
    <item>
      <title>Re: Retain with colon :</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-with-colon/m-p/958565#M374117</link>
      <description>&lt;P&gt;Provide an example of what you expect the "retained" output data set to look like. What you have provided makes no sense to "retain" anything. With a source data set like this:&lt;/P&gt;
&lt;PRE&gt;Data have;
Input CustID Health2024 Wealth2024  Health2023 Wealth2023;
cards;
1 100 20 200 15
2 150 30 300 18
;&lt;/PRE&gt;
&lt;P&gt;there would be no reason, at least unless a lot of explanation is provided, that the values from CustId=1 would be retained for use with CustId=2.&lt;/P&gt;
&lt;P&gt;The way RETAIN works any variable that is existing in a data set would be replaced by the version on the next observation if the NAME of the variable is the same.&lt;/P&gt;</description>
      <pubDate>Thu, 06 Feb 2025 16:29:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-with-colon/m-p/958565#M374117</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2025-02-06T16:29:26Z</dc:date>
    </item>
    <item>
      <title>Re: Retain with colon :</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-with-colon/m-p/958770#M374174</link>
      <description>&lt;P&gt;If you want to re-order pre-existing variables using the trailing colon naming convention, don't bother with retain.&amp;nbsp; Use "keep=" in a MERGE statement, which (in the absence of a BY statement) does a 1 to 1 merge.&amp;nbsp; That's a 1 to 1 "self-merge" in this case.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input CustID Health2024 Wealth2024  Health2023 Wealth2023;
cards;
1 100 20 200 15
2 150 30 300 18
run;

data want;
  merge have (keep=custid health:) have ;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If there are other variables you want to follow custid and health variables and wealth variables, then you could add another argument to the merge statement:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  merge have (keep=custid health:)  have (keep=wealth:)  have;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 09 Feb 2025 02:24:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-with-colon/m-p/958770#M374174</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2025-02-09T02:24:03Z</dc:date>
    </item>
  </channel>
</rss>

