<?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: Retaining values when some subsequent values are missing (SAS Base 9.4) in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Retaining-values-when-some-subsequent-values-are-missing-SAS/m-p/922988#M363396</link>
    <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/17916"&gt;@rogerward&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can also use the RETAIN statement:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
newcounter=ifn(id=lag(id),coalesce(counter,newcounter),counter);
retain newcounter;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The code can be simplified if (as in your sample data) the COUNTER of the first observation of an ID is never missing:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
newcounter=coalesce(counter,newcounter);
retain newcounter;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 04 Apr 2024 14:31:57 GMT</pubDate>
    <dc:creator>FreelanceReinh</dc:creator>
    <dc:date>2024-04-04T14:31:57Z</dc:date>
    <item>
      <title>Retaining values when some subsequent values are missing (SAS Base 9.4)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retaining-values-when-some-subsequent-values-are-missing-SAS/m-p/922645#M363308</link>
      <description>&lt;P&gt;Hi groupies,&lt;/P&gt;
&lt;P&gt;I am struggling with this seemingly simple retain statement.&lt;/P&gt;
&lt;P&gt;In the first data step (called HAVE), the variable COUNTER is sometimes missing.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When it is missing, per ID, I want (as shown in the second data step) NEWCOUNTER to be equal to the retained value of COUNTER.&amp;nbsp; In other words, if COUNTER is missing, I want NEWCOUNTER to be equal to the previous value previous non-missing value of COUNTER.&lt;/P&gt;
&lt;P&gt;The following two data steps represent what I HAVE and what I WANT.&lt;/P&gt;
&lt;P&gt;Thanks a lot for your help!&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data have;&lt;BR /&gt;input id counter;&lt;BR /&gt;datalines;&lt;BR /&gt;1 1 &lt;BR /&gt;1 2 &lt;BR /&gt;2 1 &lt;BR /&gt;3 1 &lt;BR /&gt;3 . &lt;BR /&gt;3 2 &lt;BR /&gt;3 . &lt;BR /&gt;3 . &lt;BR /&gt;3 3 &lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;data want;&lt;BR /&gt;input id counter newcounter;&lt;BR /&gt;datalines;&lt;BR /&gt;1 1 1&lt;BR /&gt;1 2 2&lt;BR /&gt;2 1 1&lt;BR /&gt;3 1 1&lt;BR /&gt;3 . 1&lt;BR /&gt;3 2 2&lt;BR /&gt;3 . 2&lt;BR /&gt;3 . 2&lt;BR /&gt;3 3 3&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Wed, 03 Apr 2024 00:15:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retaining-values-when-some-subsequent-values-are-missing-SAS/m-p/922645#M363308</guid>
      <dc:creator>rogerward</dc:creator>
      <dc:date>2024-04-03T00:15:16Z</dc:date>
    </item>
    <item>
      <title>Re: Retaining values when some subsequent values are missing (SAS Base 9.4)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retaining-values-when-some-subsequent-values-are-missing-SAS/m-p/922967#M363392</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id counter;
rowid=_N_;
datalines;
1 1
1 2
2 1
3 1
3 .
3 2
3 .
3 .
3 3
;
run;

proc expand data=have
            out=want
            METHOD=STEP;
  id rowid;
  convert counter = newcounter ;
run;
/* end of program */&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;PROC EXPAND is a SAS/ETS procedure.&lt;BR /&gt;Koen&lt;/P&gt;</description>
      <pubDate>Thu, 04 Apr 2024 13:41:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retaining-values-when-some-subsequent-values-are-missing-SAS/m-p/922967#M363392</guid>
      <dc:creator>sbxkoenk</dc:creator>
      <dc:date>2024-04-04T13:41:05Z</dc:date>
    </item>
    <item>
      <title>Re: Retaining values when some subsequent values are missing (SAS Base 9.4)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retaining-values-when-some-subsequent-values-are-missing-SAS/m-p/922971#M363393</link>
      <description>&lt;P&gt;The update statement can be used to carry forward the value of counter.&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 id counter;
   datalines;
1 1
1 2
2 1
3 1
3 .
3 2
3 .
3 .
3 3
;
run;

data want;
   if 0 then set have;
   update have(obs=0 keep=id) have(rename=(counter=newcounter));
   by id;
   set have(keep=counter);
   output;
   run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture.PNG" style="width: 288px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/95206iE91E74B000194AD4/image-size/large?v=v2&amp;amp;px=999" role="button" title="Capture.PNG" alt="Capture.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 04 Apr 2024 13:53:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retaining-values-when-some-subsequent-values-are-missing-SAS/m-p/922971#M363393</guid>
      <dc:creator>data_null__</dc:creator>
      <dc:date>2024-04-04T13:53:49Z</dc:date>
    </item>
    <item>
      <title>Re: Retaining values when some subsequent values are missing (SAS Base 9.4)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retaining-values-when-some-subsequent-values-are-missing-SAS/m-p/922973#M363394</link>
      <description>&lt;P&gt;Thanks a lot, data_null__ ! It is very clever.&lt;/P&gt;</description>
      <pubDate>Thu, 04 Apr 2024 14:06:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retaining-values-when-some-subsequent-values-are-missing-SAS/m-p/922973#M363394</guid>
      <dc:creator>rogerward</dc:creator>
      <dc:date>2024-04-04T14:06:45Z</dc:date>
    </item>
    <item>
      <title>Re: Retaining values when some subsequent values are missing (SAS Base 9.4)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retaining-values-when-some-subsequent-values-are-missing-SAS/m-p/922975#M363395</link>
      <description>Thanks, Koen.  I have never used PROC EXPAND.  I need to expand my life.</description>
      <pubDate>Thu, 04 Apr 2024 14:08:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retaining-values-when-some-subsequent-values-are-missing-SAS/m-p/922975#M363395</guid>
      <dc:creator>rogerward</dc:creator>
      <dc:date>2024-04-04T14:08:06Z</dc:date>
    </item>
    <item>
      <title>Re: Retaining values when some subsequent values are missing (SAS Base 9.4)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retaining-values-when-some-subsequent-values-are-missing-SAS/m-p/922988#M363396</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/17916"&gt;@rogerward&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can also use the RETAIN statement:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
newcounter=ifn(id=lag(id),coalesce(counter,newcounter),counter);
retain newcounter;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The code can be simplified if (as in your sample data) the COUNTER of the first observation of an ID is never missing:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
newcounter=coalesce(counter,newcounter);
retain newcounter;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 04 Apr 2024 14:31:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retaining-values-when-some-subsequent-values-are-missing-SAS/m-p/922988#M363396</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2024-04-04T14:31:57Z</dc:date>
    </item>
    <item>
      <title>Re: Retaining values when some subsequent values are missing (SAS Base 9.4)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retaining-values-when-some-subsequent-values-are-missing-SAS/m-p/923066#M363424</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id counter;
datalines;
1 1
1 2
2 1
3 1
3 .
3 2
3 .
3 .
3 3
;
data want (drop=_:);
  set have;
  by id;
  retain _ctr;
  if first.id=1 or counter^=. then _ctr=counter;
  else counter=_ctr; 
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 05 Apr 2024 00:45:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retaining-values-when-some-subsequent-values-are-missing-SAS/m-p/923066#M363424</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2024-04-05T00:45:33Z</dc:date>
    </item>
  </channel>
</rss>

