<?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 in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Retain/m-p/665203#M198860</link>
    <description>&lt;P&gt;ord+1 gets only executed when the condition is true. That's why &lt;EM&gt;ord&lt;/EM&gt; has the same value within your by group.&lt;/P&gt;
&lt;P&gt;And yes, &lt;EM&gt;ord&lt;/EM&gt; is not missing for the 2nd to nth value within a by group because it is retained. In your code you could even remove the Retain statement and the value would still be retained because SAS syntax &lt;EM&gt;ord+1&lt;/EM&gt; also implicitly leads to a retain. ...but: If the value wouldn't be retained then your counter wouldn't work at all as in the first case you would add +1 to a missing.&lt;/P&gt;
&lt;P&gt;Also to be considered: Depending on the syntax adding +1 to a missing value returns either missing or 1.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here the result from multiple syntax variations.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data dt2;
  set dt;
  by dat;
  retain ord1 ord2 ord3;
  retain ord2_B 1;
  if first.dat then 
    do;
      ord1+1;
      ord2=ord2+1;
      ord2_B=ord2_B+1;
      ord3=sum(ord3,1);
      ord4+1;
      ord5=ord5+1;
      ord6=sum(ord6,1);
    end;
run;

proc print data=dt2;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1593131310987.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/46678i56B00DC92D2900B7/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Patrick_0-1593131310987.png" alt="Patrick_0-1593131310987.png" /&gt;&lt;/span&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;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 26 Jun 2020 00:32:59 GMT</pubDate>
    <dc:creator>Patrick</dc:creator>
    <dc:date>2020-06-26T00:32:59Z</dc:date>
    <item>
      <title>Retain</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain/m-p/665199#M198857</link>
      <description>&lt;P&gt;Hello, I am trying to generate same values for by group ie.&lt;/P&gt;&lt;P&gt;abc&amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp;&lt;BR /&gt;abc&amp;nbsp; &amp;nbsp; 1&lt;BR /&gt;abc&amp;nbsp; &amp;nbsp; 1&lt;BR /&gt;def&amp;nbsp; &amp;nbsp; 2&lt;BR /&gt;def&amp;nbsp; &amp;nbsp; 2&lt;BR /&gt;xyz&amp;nbsp; &amp;nbsp;3&lt;BR /&gt;xyz&amp;nbsp; &amp;nbsp;3&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I used the following code:&lt;/P&gt;&lt;P&gt;data dt;&lt;BR /&gt;input dat $;&lt;BR /&gt;datalines;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;abc&lt;/P&gt;&lt;P&gt;abc&lt;/P&gt;&lt;P&gt;xyz&lt;/P&gt;&lt;P&gt;abc&lt;/P&gt;&lt;P&gt;def&lt;/P&gt;&lt;P&gt;xyz&lt;/P&gt;&lt;P&gt;def&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;proc sort data=dt;&lt;BR /&gt;by dat;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;data dt2;&lt;BR /&gt;set dt;&lt;BR /&gt;by dat;&lt;BR /&gt;retain ord;&lt;BR /&gt;if first.dat then ord+1;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It does work well, but I was wondering since I used just first.dat, I was thinking the ord+1 will only apply to first value per the by group. However, it showed up for all values. Is this because of retain statement? I understand retain statement holds the value but how is it applying the ord+1 for values which are not first in the by group? Please help.&lt;/P&gt;</description>
      <pubDate>Fri, 26 Jun 2020 00:02:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain/m-p/665199#M198857</guid>
      <dc:creator>Tommer</dc:creator>
      <dc:date>2020-06-26T00:02:03Z</dc:date>
    </item>
    <item>
      <title>Re: Retain</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain/m-p/665203#M198860</link>
      <description>&lt;P&gt;ord+1 gets only executed when the condition is true. That's why &lt;EM&gt;ord&lt;/EM&gt; has the same value within your by group.&lt;/P&gt;
&lt;P&gt;And yes, &lt;EM&gt;ord&lt;/EM&gt; is not missing for the 2nd to nth value within a by group because it is retained. In your code you could even remove the Retain statement and the value would still be retained because SAS syntax &lt;EM&gt;ord+1&lt;/EM&gt; also implicitly leads to a retain. ...but: If the value wouldn't be retained then your counter wouldn't work at all as in the first case you would add +1 to a missing.&lt;/P&gt;
&lt;P&gt;Also to be considered: Depending on the syntax adding +1 to a missing value returns either missing or 1.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here the result from multiple syntax variations.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data dt2;
  set dt;
  by dat;
  retain ord1 ord2 ord3;
  retain ord2_B 1;
  if first.dat then 
    do;
      ord1+1;
      ord2=ord2+1;
      ord2_B=ord2_B+1;
      ord3=sum(ord3,1);
      ord4+1;
      ord5=ord5+1;
      ord6=sum(ord6,1);
    end;
run;

proc print data=dt2;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1593131310987.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/46678i56B00DC92D2900B7/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Patrick_0-1593131310987.png" alt="Patrick_0-1593131310987.png" /&gt;&lt;/span&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;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 26 Jun 2020 00:32:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain/m-p/665203#M198860</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2020-06-26T00:32:59Z</dc:date>
    </item>
    <item>
      <title>Re: Retain</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain/m-p/665206#M198862</link>
      <description>Thanks! I applied the same logic of retain to do this where I want the same num values for each by group to be populated under ord. But now I don't see the num being carried over. What am I missing?&lt;BR /&gt;&lt;BR /&gt;data dt;&lt;BR /&gt;infile datalines missover;&lt;BR /&gt;input dat $ num;&lt;BR /&gt;datalines;&lt;BR /&gt;abc 1&lt;BR /&gt;abc&lt;BR /&gt;xyz 2&lt;BR /&gt;abc&lt;BR /&gt;def 3&lt;BR /&gt;xyz&lt;BR /&gt;def&lt;BR /&gt;;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;proc sort data=dt;&lt;BR /&gt;by dat;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;data dt2;&lt;BR /&gt;set dt;&lt;BR /&gt;by dat;&lt;BR /&gt;retain num;&lt;BR /&gt;if first.dat then ord=num;&lt;BR /&gt;run;</description>
      <pubDate>Fri, 26 Jun 2020 00:36:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain/m-p/665206#M198862</guid>
      <dc:creator>Tommer</dc:creator>
      <dc:date>2020-06-26T00:36:10Z</dc:date>
    </item>
    <item>
      <title>Re: Retain</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain/m-p/665211#M198864</link>
      <description>&lt;P&gt;You are missing the fact that you added NUM to&amp;nbsp; your incoming data set.&amp;nbsp; That means each time the SET statement reads in an observation, it replaces the previous value for NUM.&amp;nbsp; If the new value is missing, it replaces a nonmissing value.&amp;nbsp; You should not have NUM in your incoming data to apply this logic (which is otherwise perfectly good logic).&lt;/P&gt;</description>
      <pubDate>Fri, 26 Jun 2020 01:01:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain/m-p/665211#M198864</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2020-06-26T01:01:00Z</dc:date>
    </item>
    <item>
      <title>Re: Retain</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain/m-p/665243#M198878</link>
      <description>&lt;P&gt;As&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4954"&gt;@Astounding&lt;/a&gt;&amp;nbsp;noted, the fact that NUM in in the input dataset means that any RETAIN instruction you issued is superseded by the fact that NUM is read in for each iteration of the DATA set.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And in fact, you don't even need a retain statement for variables that are read via SET or MERGE - they are automatically retained, but it's not usually apparent.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can use this attribute of SAS and produce the result you are looking for by using a conditional SET statement for the NUM variable, as in:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data dt;
  infile datalines missover;
  input dat $ num;
datalines;
abc 1
abc
xyz 2
abc
def 3
xyz
def
run;

proc sort data=dt;
  by dat;
run;


data dt2;
  set dt (drop=num);
  by dat;
  if first.dat then set dt (keep=num) point=_n_;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This depends on two properties.&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;&amp;nbsp; The proc sort retains the original record order within by groups.&amp;nbsp; And since you issue a NUM value for the first record of each DAT value in the original unsorted data set, the sorted dataset will have the desired NUM at the beginning of each by group.&lt;BR /&gt;&lt;BR /&gt;&lt;/LI&gt;
&lt;LI&gt;The unconditional SET statement doesn't read in NUM, but the subsequent conditional SET (if first.dat then set ...) does read in NUM.&amp;nbsp; This value will be automatically retained until the next time the condition is satisfied.&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It's not particularly recommended for you problem, but this conditional SET technique is very useful for "bringing history forward".&amp;nbsp; You can read in, say, chronologically sorted monthly data set, a quarterly data set, and a yearly data set, such that the most recent yearly and quarterly values are brought forward to each monthly record.&lt;/P&gt;</description>
      <pubDate>Fri, 26 Jun 2020 05:12:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain/m-p/665243#M198878</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2020-06-26T05:12:23Z</dc:date>
    </item>
    <item>
      <title>Re: Retain</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain/m-p/665385#M198953</link>
      <description>&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12447"&gt;@Patrick&lt;/a&gt; these are all great examples with really good scenarios to understand the concepts better!</description>
      <pubDate>Fri, 26 Jun 2020 15:19:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain/m-p/665385#M198953</guid>
      <dc:creator>Tommer</dc:creator>
      <dc:date>2020-06-26T15:19:46Z</dc:date>
    </item>
  </channel>
</rss>

