<?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 within group with condition in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Retain-within-group-with-condition/m-p/902428#M356631</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input id key_value v1 v2 v3;
  datalines;
1 . . . .
1 6 5 3 .
1 . . . .
1 7 8 . .
1 . . . .
2 9 . . 9
2 . . . .
2 1 . 7 .
2 . . . .
;

data temp;
 set have;
 by id;
 if first.id or not missing(key_value) then group+1;
 keep id group v:;
run;
data temp2;
 update temp(obs=0) temp;
 by id group;
 output;
run;
data want;
 merge have(keep=id key_value) temp2(keep=v:);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 10 Nov 2023 01:17:39 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2023-11-10T01:17:39Z</dc:date>
    <item>
      <title>Retain within group with condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-within-group-with-condition/m-p/902420#M356627</link>
      <description>&lt;P&gt;Hi Everyone,&lt;/P&gt;
&lt;P&gt;My data has ID, Key_Value, v1,v2,v3.&lt;/P&gt;
&lt;P&gt;In the data, when Key_value ^=. , at least 1 of v1,v2,v3 will have value.&lt;/P&gt;
&lt;P&gt;I want to fill all missing value of v1, v2, v3 with the corresponding value associated with the most recent available Key_value.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The tricky part is that retain will fill value all the way until it reach non-missing value of the same column (instead of the Key_value column).&lt;/P&gt;
&lt;P&gt;The first few rows desirable output should look like where for V2 =3 only fills up to 3rd record.&lt;/P&gt;
&lt;P&gt;Can you please help?&lt;/P&gt;
&lt;P&gt;Many thanks,&lt;/P&gt;
&lt;P&gt;HHC&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;1 . . . .
1 6 5 3 .
1 . 5 &lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;&lt;U&gt;3&lt;/U&gt;&lt;/STRONG&gt;&lt;/FONT&gt; .
1 7 8 . ./*No fill of value 3 here as Key_value contain non-missing*/
1 . 8 . .&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Input file&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input id key_value v1 v2 v3;
  datalines;
1 . . . .
1 6 5 3 .
1 . . . .
1 7 8 . .
1 . . . .
2 9 . . 9
2 . . . .
2 1 . 7 .
2 . . . .
;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 09 Nov 2023 21:43:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-within-group-with-condition/m-p/902420#M356627</guid>
      <dc:creator>hhchenfx</dc:creator>
      <dc:date>2023-11-09T21:43:24Z</dc:date>
    </item>
    <item>
      <title>Re: Retain within group with condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-within-group-with-condition/m-p/902423#M356628</link>
      <description>&lt;P&gt;How about this code?&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (rename= (_v1=v1 _v2=v2 _v3=v3));
	set have;
	by id; 
	retain _v1 _v2 _v3;
	array A [3] v1-v3;
	array B [3] _v1-_v3;
	do i= 1 to 3;
		if key_value^=.  then B{i}=A{i};
	end;
	drop i v:;
proc print; run; &lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 09 Nov 2023 22:23:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-within-group-with-condition/m-p/902423#M356628</guid>
      <dc:creator>A_Kh</dc:creator>
      <dc:date>2023-11-09T22:23:18Z</dc:date>
    </item>
    <item>
      <title>Re: Retain within group with condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-within-group-with-condition/m-p/902424#M356629</link>
      <description>&lt;P&gt;I am having a very hard time trying to figure out why doing something like this makes any sense.&lt;/P&gt;
&lt;P&gt;But this method works for your first ID that you showed example output for.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can use UPDATE statement to do last observation carried forward.&lt;/P&gt;
&lt;P&gt;Then you should re-read the value of KEY_VALUE so it is not carried forward.&amp;nbsp; And when KEY_VALUE is not empty then re-read the whole observation so none of the variables are carried forward.&amp;nbsp; Also add an explicit OUTPUT statement so you get back out the same number of observations instead of just one per ID value.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input id key_value v1 v2 v3;
datalines;
1 . . . .
1 6 5 3 .
1 . . . .
1 7 8 . .
1 . . . .
2 9 . . 9
2 . . . .
2 1 . 7 .
2 . . . .
;

data want;
  update have(obs=0) have;
  by id;
  set have(keep=key_value);
  if not missing(key_value) then set have point=_n_;
  output;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results&lt;/P&gt;
&lt;PRE&gt;              key_
Obs    id    value    v1    v2    v3

 1      1      .       .     .     .
 2      1      6       5     3     .
 3      1      .       5     3     .
 4      1      7       8     .     .
 5      1      .       8     .     .
 6      2      9       .     .     9
 7      2      .       .     .     9
 8      2      1       .     7     .
 9      2      .       .     7     .
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 09 Nov 2023 23:28:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-within-group-with-condition/m-p/902424#M356629</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-11-09T23:28:56Z</dc:date>
    </item>
    <item>
      <title>Re: Retain within group with condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-within-group-with-condition/m-p/902428#M356631</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input id key_value v1 v2 v3;
  datalines;
1 . . . .
1 6 5 3 .
1 . . . .
1 7 8 . .
1 . . . .
2 9 . . 9
2 . . . .
2 1 . 7 .
2 . . . .
;

data temp;
 set have;
 by id;
 if first.id or not missing(key_value) then group+1;
 keep id group v:;
run;
data temp2;
 update temp(obs=0) temp;
 by id group;
 output;
run;
data want;
 merge have(keep=id key_value) temp2(keep=v:);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 10 Nov 2023 01:17:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-within-group-with-condition/m-p/902428#M356631</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2023-11-10T01:17:39Z</dc:date>
    </item>
    <item>
      <title>Re: Retain within group with condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-within-group-with-condition/m-p/902432#M356634</link>
      <description>&lt;P&gt;Thanks, Tom!&lt;/P&gt;
&lt;P&gt;Your response makes my day &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;HHC&lt;/P&gt;</description>
      <pubDate>Fri, 10 Nov 2023 02:45:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-within-group-with-condition/m-p/902432#M356634</guid>
      <dc:creator>hhchenfx</dc:creator>
      <dc:date>2023-11-10T02:45:56Z</dc:date>
    </item>
  </channel>
</rss>

