<?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 and impute in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/RETAIN-and-impute/m-p/790769#M253199</link>
    <description>&lt;P&gt;it's not the most effcient code.&lt;/P&gt;
&lt;P&gt;I do not manage to save the sort operation not even by putting tjhe notsorted option in the data step.&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 day flag;
cards;
1   1     .
1   1     2
2   4     2
2   4     .
2   5     .
3   3     2
3   3     .
3   3     8
;
run;

data have;
set have;
helper=missing(flag);
run;

proc sort data=have;
by id day helper;
run;

data want;
set have;
by id day helper;
retain lasty;
lasty=lag(flag);
if first.day then call missing(lasty);
if missing(flag) then flag=lasty;
drop helper lasty;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 18 Jan 2022 22:02:38 GMT</pubDate>
    <dc:creator>acordes</dc:creator>
    <dc:date>2022-01-18T22:02:38Z</dc:date>
    <item>
      <title>RETAIN and impute</title>
      <link>https://communities.sas.com/t5/SAS-Programming/RETAIN-and-impute/m-p/790765#M253196</link>
      <description>&lt;P&gt;I have the following data--how can I impute within the same ID and DAY the flag variable--if multiple flag values are available then take the first value and impute within the same ID and DAY--see example ID=3 and DAY=3:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data have;&lt;BR /&gt;input id day flag;&lt;BR /&gt;cards;&lt;BR /&gt;1&amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp;.&lt;BR /&gt;1&amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp;2&lt;BR /&gt;2&amp;nbsp; &amp;nbsp;4&amp;nbsp; &amp;nbsp; &amp;nbsp;2&lt;BR /&gt;2&amp;nbsp; &amp;nbsp;4&amp;nbsp; &amp;nbsp; &amp;nbsp;.&lt;BR /&gt;2&amp;nbsp; &amp;nbsp;5&amp;nbsp; &amp;nbsp; &amp;nbsp;.&lt;BR /&gt;3&amp;nbsp; &amp;nbsp;3&amp;nbsp; &amp;nbsp; &amp;nbsp;2&lt;BR /&gt;3&amp;nbsp; &amp;nbsp;3&amp;nbsp; &amp;nbsp; &amp;nbsp;.&lt;BR /&gt;3&amp;nbsp; &amp;nbsp;3&amp;nbsp; &amp;nbsp; &amp;nbsp;8&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data wanted;&lt;BR /&gt;input id day flag;&lt;BR /&gt;cards;&lt;BR /&gt;1&amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp;2&lt;BR /&gt;1&amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp;2&lt;BR /&gt;2&amp;nbsp; &amp;nbsp;4&amp;nbsp; &amp;nbsp; &amp;nbsp;2&lt;BR /&gt;2&amp;nbsp; &amp;nbsp;4&amp;nbsp; &amp;nbsp; &amp;nbsp;2&lt;BR /&gt;2&amp;nbsp; &amp;nbsp;5&amp;nbsp; &amp;nbsp; &amp;nbsp;.&lt;BR /&gt;3&amp;nbsp; &amp;nbsp;3&amp;nbsp; &amp;nbsp; &amp;nbsp;2&lt;BR /&gt;3&amp;nbsp; &amp;nbsp;3&amp;nbsp; &amp;nbsp; &amp;nbsp;2&lt;BR /&gt;3&amp;nbsp; &amp;nbsp;3&amp;nbsp; &amp;nbsp; &amp;nbsp;8&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 18 Jan 2022 21:40:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/RETAIN-and-impute/m-p/790765#M253196</guid>
      <dc:creator>Emma2021</dc:creator>
      <dc:date>2022-01-18T21:40:11Z</dc:date>
    </item>
    <item>
      <title>Re: RETAIN and impute</title>
      <link>https://communities.sas.com/t5/SAS-Programming/RETAIN-and-impute/m-p/790768#M253198</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
input id day flag;
cards;
1   1     .
1   1     2
2   4     2
2   4     .
2   5     .
3   3     2
3   3     .
3   3     8
;

data want ;
  do until (last.day) ;
    set have ;
	by id day ;
	if flag and _n=. then _n = flag ;
  end ;
  do until (last.day) ;
    set have ;
	by id day ;
	if flag=. then flag = _n ;
	output ;
  end ;
  drop _n ;
run ;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 18 Jan 2022 21:54:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/RETAIN-and-impute/m-p/790768#M253198</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2022-01-18T21:54:31Z</dc:date>
    </item>
    <item>
      <title>Re: RETAIN and impute</title>
      <link>https://communities.sas.com/t5/SAS-Programming/RETAIN-and-impute/m-p/790769#M253199</link>
      <description>&lt;P&gt;it's not the most effcient code.&lt;/P&gt;
&lt;P&gt;I do not manage to save the sort operation not even by putting tjhe notsorted option in the data step.&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 day flag;
cards;
1   1     .
1   1     2
2   4     2
2   4     .
2   5     .
3   3     2
3   3     .
3   3     8
;
run;

data have;
set have;
helper=missing(flag);
run;

proc sort data=have;
by id day helper;
run;

data want;
set have;
by id day helper;
retain lasty;
lasty=lag(flag);
if first.day then call missing(lasty);
if missing(flag) then flag=lasty;
drop helper lasty;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 18 Jan 2022 22:02:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/RETAIN-and-impute/m-p/790769#M253199</guid>
      <dc:creator>acordes</dc:creator>
      <dc:date>2022-01-18T22:02:38Z</dc:date>
    </item>
    <item>
      <title>Re: RETAIN and impute</title>
      <link>https://communities.sas.com/t5/SAS-Programming/RETAIN-and-impute/m-p/790770#M253200</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
input id day flag;
cards;
1   1     .
1   1     2
2   4     2
2   4     .
2   5     .
3   3     2
3   3     .
3   3     8
;

data want ;
  if _n_ = 1 then do ;
    dcl hash h (dataset:'have(where=(flag))' ) ;
	h.definekey ('id', 'day') ;
	h.definedata ('flag') ;
	h.definedone ( ) ;
  end ;
  set have ;
  if flag=. then _n_ = h.find( ) ;
run ;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 18 Jan 2022 22:10:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/RETAIN-and-impute/m-p/790770#M253200</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2022-01-18T22:10:05Z</dc:date>
    </item>
    <item>
      <title>Re: RETAIN and impute</title>
      <link>https://communities.sas.com/t5/SAS-Programming/RETAIN-and-impute/m-p/790773#M253201</link>
      <description>Is it possible to change this SAS code to SQL (maybe mysql or any SQL)?&lt;BR /&gt;Thank you!</description>
      <pubDate>Tue, 18 Jan 2022 22:18:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/RETAIN-and-impute/m-p/790773#M253201</guid>
      <dc:creator>Emma2021</dc:creator>
      <dc:date>2022-01-18T22:18:18Z</dc:date>
    </item>
    <item>
      <title>Re: RETAIN and impute</title>
      <link>https://communities.sas.com/t5/SAS-Programming/RETAIN-and-impute/m-p/790774#M253202</link>
      <description>I'd actually recommend you look into PROC EXPAND and the different options for imputation for time series. You can get some different options using that procedure. You do need a license for SAS/ETS though. &lt;BR /&gt;&lt;BR /&gt;SQL is typically not a good solution for this type of problem and it requires more intensive computations as you're joining data sets with itself. What's your rational for trying to use SQL instead of data steps?</description>
      <pubDate>Tue, 18 Jan 2022 22:36:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/RETAIN-and-impute/m-p/790774#M253202</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2022-01-18T22:36:31Z</dc:date>
    </item>
    <item>
      <title>Re: RETAIN and impute</title>
      <link>https://communities.sas.com/t5/SAS-Programming/RETAIN-and-impute/m-p/790777#M253204</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
proc sql;
    create table WANT(drop=flag rename=(flag_=flag)) as
   select id,day,flag,max(flag) as flag_
   from have
   group by 1,2;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 18 Jan 2022 22:44:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/RETAIN-and-impute/m-p/790777#M253204</guid>
      <dc:creator>r_behata</dc:creator>
      <dc:date>2022-01-18T22:44:28Z</dc:date>
    </item>
    <item>
      <title>Re: RETAIN and impute</title>
      <link>https://communities.sas.com/t5/SAS-Programming/RETAIN-and-impute/m-p/790781#M253205</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/381519"&gt;@Emma2021&lt;/a&gt;&amp;nbsp; &amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;&amp;nbsp;'s recommendation is spot on and very wise. Right, I am just posting for fun and to get my fingers back into some practice. I'd still suggest to go with Reeza's suggestion or use a DB specific SQL making use of the PARTIITON BY() option, enabling the use of min from each partition.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Anyways, for fun-&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
input id day flag;
cards;
1   1     .
1   1     2
2   4     2
2   4     .
2   5     .
3   3     2
3   3     .
3   3     8
;


proc sql;
  create table want as
  select a.id,a.day,coalesce(a.flag,b.flag) as flag
  from have a
  left join 
  (select *
  from (select *,monotonic() as n from have)
  group by id, day
  having min(case when flag&amp;gt;. then n else . end)=n) b
  on a.id=b.id and a.day=b.day;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 18 Jan 2022 23:12:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/RETAIN-and-impute/m-p/790781#M253205</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2022-01-18T23:12:27Z</dc:date>
    </item>
    <item>
      <title>Re: RETAIN and impute</title>
      <link>https://communities.sas.com/t5/SAS-Programming/RETAIN-and-impute/m-p/790812#M253214</link>
      <description>Thank you so much. &lt;BR /&gt;I have oracle sql. Would you change the sas sql into oracle sql? &lt;BR /&gt;Thanks again!</description>
      <pubDate>Wed, 19 Jan 2022 06:22:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/RETAIN-and-impute/m-p/790812#M253214</guid>
      <dc:creator>Emma2021</dc:creator>
      <dc:date>2022-01-19T06:22:48Z</dc:date>
    </item>
    <item>
      <title>Re: RETAIN and impute</title>
      <link>https://communities.sas.com/t5/SAS-Programming/RETAIN-and-impute/m-p/790817#M253217</link>
      <description>&lt;P&gt;I suggest you ask your local Oracle developers/admins.&lt;/P&gt;</description>
      <pubDate>Wed, 19 Jan 2022 07:04:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/RETAIN-and-impute/m-p/790817#M253217</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-01-19T07:04:26Z</dc:date>
    </item>
    <item>
      <title>Re: RETAIN and impute</title>
      <link>https://communities.sas.com/t5/SAS-Programming/RETAIN-and-impute/m-p/790875#M253241</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id day flag;
cards;
1   1     .
1   1     2
2   4     2
2   4     .
2   5     .
3   3     .
3   3     2
3   3     .
3   3     8
3   3     .
3   3     88
3   3     .
;

data temp;
 set have;
 n+1;
run;
data temp1;
 set temp;
 by id day;
 retain new_flag;
 if first.day then call missing(new_flag);
 if not missing(flag) then new_flag=flag;
run;
proc sort data=temp1;by descending n;run;

data want;
 set temp1;
 by id  day notsorted;
 retain new_flag2;
 if first.day then call missing(new_flag2);
 if not missing(new_flag) then new_flag2=new_flag;
 drop new_flag ;
run;
proc sort data=want;by  n;run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 19 Jan 2022 12:50:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/RETAIN-and-impute/m-p/790875#M253241</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2022-01-19T12:50:57Z</dc:date>
    </item>
  </channel>
</rss>

