<?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: fill gap between records in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/fill-gap-between-records/m-p/452162#M283852</link>
    <description>&lt;P&gt;This program uses the lag function, which updates lagged value of visit only&amp;nbsp; once per visit "group", i.e. only&amp;nbsp; at the end of a sequence of identical&amp;nbsp; visit values.&amp;nbsp;&amp;nbsp; If the lagged value is missing, and the double-lagged value matches the current value then set _FILLIN=1.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Reread the records read above, and if _FILLIN=1 then replace missing visit values with the double-lagged value.&amp;nbsp; After the&amp;nbsp; re-read output the records:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (drop=_:);
  do _N=1 by 1 until (last.id or _visit_lag1=.);
    set one;
    by id visit notsorted;
    if last.visit then do;
      _visit_lag1=lag1(visit);
      _visit_lag2=lag2(visit);
      _fillin= (visit=_visit_lag2);
    end;
  end;
  do _I=1 to _N;
    set one;
    if visit=. and _fillin=1 then visit=_visit_lag2;
    output;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Sat, 07 Apr 2018 04:23:18 GMT</pubDate>
    <dc:creator>mkeintz</dc:creator>
    <dc:date>2018-04-07T04:23:18Z</dc:date>
    <item>
      <title>fill gap between records</title>
      <link>https://communities.sas.com/t5/SAS-Programming/fill-gap-between-records/m-p/451942#M283841</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;how do i fill gaps between records. since there is gap between visit 1 and visit 2 i dont want those gaps to be filled but i want gaps filled between same visit like there is gap between visit 2, so that gap should be filled with same visit thats 2.&lt;/P&gt;&lt;P&gt;data one;&lt;BR /&gt;input id $3. visit;&lt;BR /&gt;datalines;&lt;BR /&gt;101 1&lt;BR /&gt;101 1&lt;BR /&gt;101 .&lt;BR /&gt;101 .&lt;BR /&gt;101 2&lt;BR /&gt;101 .&lt;BR /&gt;101 2&lt;BR /&gt;101 3&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;final output expected should be&amp;nbsp;&lt;/P&gt;&lt;P&gt;101 1&lt;BR /&gt;101 1&lt;BR /&gt;101 .&lt;BR /&gt;101 .&lt;BR /&gt;101 2&lt;BR /&gt;101 2&amp;nbsp;&lt;BR /&gt;101 2&lt;BR /&gt;101 3&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;thanks,&lt;/P&gt;&lt;P&gt;er&lt;/P&gt;</description>
      <pubDate>Fri, 06 Apr 2018 15:15:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/fill-gap-between-records/m-p/451942#M283841</guid>
      <dc:creator>eric2</dc:creator>
      <dc:date>2018-04-06T15:15:00Z</dc:date>
    </item>
    <item>
      <title>Re: fill gap between records</title>
      <link>https://communities.sas.com/t5/SAS-Programming/fill-gap-between-records/m-p/451964#M283842</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data one;
input id $3. visit;
datalines;
101 1
101 1
101 .
101 .
101 2
101 .
101 2
101 3
;
run;


data want;
retain _Visit;
do i=1 by 1 until(not missing(Visit_));
	set one(rename=Visit=Visit_);
	end;
do j=1 to i;
set one;
If not missing(visit) then _Visit=Visit;
if _Visit=Visit_ then Visit=_Visit;
Output;
end;
keep id visit;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I need to thank&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/462"&gt;@PGStats&lt;/a&gt;&amp;nbsp;, I learned this approach from him&amp;nbsp;&lt;img id="smileyembarrassed" class="emoticon emoticon-smileyembarrassed" src="https://communities.sas.com/i/smilies/16x16_smiley-embarrassed.png" alt="Smiley Embarassed" title="Smiley Embarassed" /&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 06 Apr 2018 16:02:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/fill-gap-between-records/m-p/451964#M283842</guid>
      <dc:creator>SuryaKiran</dc:creator>
      <dc:date>2018-04-06T16:02:18Z</dc:date>
    </item>
    <item>
      <title>Re: fill gap between records</title>
      <link>https://communities.sas.com/t5/SAS-Programming/fill-gap-between-records/m-p/451978#M283843</link>
      <description>&lt;P&gt;Thanks. Good one but if your last record is has missing visit then your total number of records are decreased by one and we want to have same number of records as original one.&lt;/P&gt;&lt;P&gt;data one;&lt;BR /&gt;input id $3. visit;&lt;BR /&gt;datalines;&lt;BR /&gt;101 1&lt;BR /&gt;101 1&lt;BR /&gt;101 .&lt;BR /&gt;101 .&lt;BR /&gt;101 2&lt;BR /&gt;101 .&lt;BR /&gt;101 2&lt;BR /&gt;101 3&lt;BR /&gt;101 .&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;thx&lt;/P&gt;</description>
      <pubDate>Fri, 06 Apr 2018 16:48:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/fill-gap-between-records/m-p/451978#M283843</guid>
      <dc:creator>eric2</dc:creator>
      <dc:date>2018-04-06T16:48:38Z</dc:date>
    </item>
    <item>
      <title>Re: fill gap between records</title>
      <link>https://communities.sas.com/t5/SAS-Programming/fill-gap-between-records/m-p/451991#M283844</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data one;
input id $3. visit;
datalines;
101 1
101 1
101 .
101 .
101 2
101 .
101 2
101 3
;
run;

data _null_;
if _n_=1 then do;
dcl hash H (multidata:'y',ordered:'a') ;
   h.definekey  ("id",'_visit') ;
   h.definedata ('id',"visit") ;
   h.definedone () ;
end;
set one end=last;
by id ;
retain _visit;
if not missing(visit) and lag(visit)=. and visit=_visit then 
h.replace(key:id,key:_visit, data:id,data: _visit);
if not missing(visit) then _visit=visit;
h.add();
if last then h.output(dataset:'want');
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 06 Apr 2018 17:18:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/fill-gap-between-records/m-p/451991#M283844</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-04-06T17:18:23Z</dc:date>
    </item>
    <item>
      <title>Re: fill gap between records</title>
      <link>https://communities.sas.com/t5/SAS-Programming/fill-gap-between-records/m-p/451996#M283845</link>
      <description>&lt;P&gt;Thanks . I am not so familiar with hash objects. How do you keep other variables if you have say more&amp;nbsp; variables in this dataset but dont use them in your logic?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;thx&lt;/P&gt;</description>
      <pubDate>Fri, 06 Apr 2018 17:29:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/fill-gap-between-records/m-p/451996#M283845</guid>
      <dc:creator>eric2</dc:creator>
      <dc:date>2018-04-06T17:29:37Z</dc:date>
    </item>
    <item>
      <title>Re: fill gap between records</title>
      <link>https://communities.sas.com/t5/SAS-Programming/fill-gap-between-records/m-p/451999#M283846</link>
      <description>&lt;P&gt;specify all of them in the definedata section as you initialize the hash obj at the top&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;  h&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;definedata &lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token string"&gt;'id'&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;&lt;SPAN class="token string"&gt;"visit"&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt; &lt;SPAN class="token punctuation"&gt;;/*and others to be included */&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;but i think this will require the &lt;STRONG&gt;data: &lt;EM&gt;in replace method&lt;/EM&gt;&lt;/STRONG&gt; to be expanded too making it a verbose&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 06 Apr 2018 17:34:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/fill-gap-between-records/m-p/451999#M283846</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-04-06T17:34:00Z</dc:date>
    </item>
    <item>
      <title>Re: fill gap between records</title>
      <link>https://communities.sas.com/t5/SAS-Programming/fill-gap-between-records/m-p/452000#M283847</link>
      <description>&lt;P&gt;Thanks . I will try it out but i wish there is a way to do it by not using hash objects.&lt;/P&gt;</description>
      <pubDate>Fri, 06 Apr 2018 17:39:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/fill-gap-between-records/m-p/452000#M283847</guid>
      <dc:creator>eric2</dc:creator>
      <dc:date>2018-04-06T17:39:12Z</dc:date>
    </item>
    <item>
      <title>Re: fill gap between records</title>
      <link>https://communities.sas.com/t5/SAS-Programming/fill-gap-between-records/m-p/452021#M283848</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/152094"&gt;@eric2&lt;/a&gt;&amp;nbsp; Ok, an easy solution for you and not to worry about variables at all:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data one;
input id $3. visit;
datalines;
101 1
101 1
101 .
101 .
101 2
101 .
101 2
101 3
101 .
;
run;
data want;
do _n_=1 by 1 until(last.id);
set one;
by id  ;
array _t(10);/*Please assign a high value subscript as mine is arbitrary/small for demo*/
if not missing(visit) and lag(visit)=. and visit=_visit then _t(_n_)=visit;
if not missing(visit) then _visit=visit;
end;
do until(last.id);
set one;
by id;
if not missing(visit) then _visit=visit;
if _visit  in _t then  visit=_visit;
output;
end;
drop _:;
run;
&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 06 Apr 2018 18:03:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/fill-gap-between-records/m-p/452021#M283848</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-04-06T18:03:59Z</dc:date>
    </item>
    <item>
      <title>Re: fill gap between records</title>
      <link>https://communities.sas.com/t5/SAS-Programming/fill-gap-between-records/m-p/452037#M283849</link>
      <description>&lt;P&gt;awesome. thanks much. this worked ....: smileyhappy:&lt;/P&gt;</description>
      <pubDate>Fri, 06 Apr 2018 18:20:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/fill-gap-between-records/m-p/452037#M283849</guid>
      <dc:creator>eric2</dc:creator>
      <dc:date>2018-04-06T18:20:29Z</dc:date>
    </item>
    <item>
      <title>Re: fill gap between records</title>
      <link>https://communities.sas.com/t5/SAS-Programming/fill-gap-between-records/m-p/452124#M283850</link>
      <description>&lt;P&gt;Covering all cases:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data one;
input id $3. visit;
datalines;
101 1
101 1
101 .
101 .
101 2
101 .
101 2
101 3
102 .
102 3
102 .
102 3
103 4
103 5
103 .
;

data want;
do until(last.id);
    do i = 1 by 1 until(last.id or not missing(Visit_));
    	set one(rename=Visit=Visit_); by id;
    	end;
    do j = 1 to i;
        set one;
        If missing(visit) then if _Visit = Visit_ then Visit = _Visit;
        Output;
        end;
    _Visit = Visit_;
    end;
keep id visit;
run;

proc print; run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 06 Apr 2018 22:12:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/fill-gap-between-records/m-p/452124#M283850</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2018-04-06T22:12:46Z</dc:date>
    </item>
    <item>
      <title>Re: fill gap between records</title>
      <link>https://communities.sas.com/t5/SAS-Programming/fill-gap-between-records/m-p/452126#M283851</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/462"&gt;@PGStats&lt;/a&gt;&amp;nbsp;Sir, may i request a min or two for a quick clarification on your very interesting statement that i want to learn and comprehend&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&lt;EM&gt;&amp;nbsp; &amp;nbsp;If missing(visit) then if _Visit = Visit_ then Visit = _Visit;&lt;/EM&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;how does the above evaluate&lt;/EM&gt;&lt;STRONG&gt;&lt;EM&gt;&amp;nbsp; if then follows if then&amp;nbsp;&lt;/EM&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;&amp;nbsp;Did i miss something not in&lt;/EM&gt;&lt;STRONG&gt;&lt;EM&gt; doc&amp;nbsp;&lt;A href="http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000202239.htm" target="_blank"&gt;http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000202239.htm&lt;/A&gt;&lt;/EM&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would appreciate your response and time. Thank you&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;EDITED at 6:35 chicago time after figuring out with some diligent reading:&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;I got the understanding. My bad I didn't realize "&lt;EM&gt;&lt;STRONG&gt;if then"&lt;/STRONG&gt;&lt;/EM&gt; is executable and is used in one&lt;EM&gt;&lt;STRONG&gt; if then&lt;/STRONG&gt;&lt;/EM&gt; without an &lt;EM&gt;&lt;STRONG&gt;if then do.&lt;/STRONG&gt;&lt;/EM&gt;&amp;nbsp;Sorry for the bother&lt;/P&gt;</description>
      <pubDate>Fri, 06 Apr 2018 23:37:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/fill-gap-between-records/m-p/452126#M283851</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-04-06T23:37:08Z</dc:date>
    </item>
    <item>
      <title>Re: fill gap between records</title>
      <link>https://communities.sas.com/t5/SAS-Programming/fill-gap-between-records/m-p/452162#M283852</link>
      <description>&lt;P&gt;This program uses the lag function, which updates lagged value of visit only&amp;nbsp; once per visit "group", i.e. only&amp;nbsp; at the end of a sequence of identical&amp;nbsp; visit values.&amp;nbsp;&amp;nbsp; If the lagged value is missing, and the double-lagged value matches the current value then set _FILLIN=1.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Reread the records read above, and if _FILLIN=1 then replace missing visit values with the double-lagged value.&amp;nbsp; After the&amp;nbsp; re-read output the records:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (drop=_:);
  do _N=1 by 1 until (last.id or _visit_lag1=.);
    set one;
    by id visit notsorted;
    if last.visit then do;
      _visit_lag1=lag1(visit);
      _visit_lag2=lag2(visit);
      _fillin= (visit=_visit_lag2);
    end;
  end;
  do _I=1 to _N;
    set one;
    if visit=. and _fillin=1 then visit=_visit_lag2;
    output;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 07 Apr 2018 04:23:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/fill-gap-between-records/m-p/452162#M283852</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2018-04-07T04:23:18Z</dc:date>
    </item>
    <item>
      <title>Re: fill gap between records</title>
      <link>https://communities.sas.com/t5/SAS-Programming/fill-gap-between-records/m-p/452183#M283853</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data one;
input id visit;
datalines;
101 1
101 1
101 .
101 .
101 2
101 .
101 2
101 3
101 .
1011 1
1011 1
1011 .
1011 .
1011 2
1011 .
1011 2
1011 3
1011 .
;
run;
data have;
 set one;
 by id;
 if first.id then n=0;
 n+1;
run;
data temp;
 set have;
 if not missing(visit);
run;
data temp1;
 merge temp temp(rename=(id=_id n=_n visit=_visit) firstobs=2);
 output;
 if id=_id then do;
  do i=n+1 to _n-1;
    if visit ne _visit then call missing(visit);
	n=i;
	output;
  end;
 end;
 drop _: i ;
run;
data want;
 merge have temp1(rename=(visit=_visit) in=inb) ;
 by id n;
 if inb then visit=_visit;
 drop _visit;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 07 Apr 2018 11:58:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/fill-gap-between-records/m-p/452183#M283853</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2018-04-07T11:58:36Z</dc:date>
    </item>
    <item>
      <title>Re: fill gap between records</title>
      <link>https://communities.sas.com/t5/SAS-Programming/fill-gap-between-records/m-p/452199#M283854</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data one;
input id visit;
datalines;
101 1
101 1
101 .
101 .
101 2
101 .
101 2
101 3
101 .
1011 1
1011 1
1011 .
1011 .
1011 2
1011 .
1011 2
1011 3
1011 .
;
run;

data temp;
 retain _visit;
 do until(last.id or not missing(visit));
   set one;
   by id;
   if first.id then call missing(_visit);
 end;
 flag=(_visit=visit);
 do until(last.id or not missing(visit));
   set one;
   by id;
   if flag then visit1=_visit;
   output;
 end;
 _visit=visit;
 drop flag _visit;
 run;
 data want;
  set temp;
  if not missing(visit1) then visit=visit1;
  drop visit1;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 07 Apr 2018 13:32:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/fill-gap-between-records/m-p/452199#M283854</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2018-04-07T13:32:58Z</dc:date>
    </item>
    <item>
      <title>Re: fill gap between records</title>
      <link>https://communities.sas.com/t5/SAS-Programming/fill-gap-between-records/m-p/452469#M283855</link>
      <description>&lt;P&gt;Here is a solution which updates the data in place:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data temp;
  set one;
  if not missing(visit);
  obsno=_N_;
  rename visit=replace_val;
run;

data one;
  set temp;
  by id replace_val notsorted;
  last_n=lag(obsno);
  if not first.replace_val then do _N_=last_n+1 to obsno-1;
    modify one point=_N_;
    visit=replace_val;
    replace;
    end;
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>Mon, 09 Apr 2018 11:40:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/fill-gap-between-records/m-p/452469#M283855</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2018-04-09T11:40:33Z</dc:date>
    </item>
    <item>
      <title>Re: fill gap between records</title>
      <link>https://communities.sas.com/t5/SAS-Programming/fill-gap-between-records/m-p/452547#M283856</link>
      <description>&lt;P&gt;Thanks all. All these worked but bit complicated code &lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 09 Apr 2018 16:23:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/fill-gap-between-records/m-p/452547#M283856</guid>
      <dc:creator>eric2</dc:creator>
      <dc:date>2018-04-09T16:23:22Z</dc:date>
    </item>
    <item>
      <title>Re: fill gap between records</title>
      <link>https://communities.sas.com/t5/SAS-Programming/fill-gap-between-records/m-p/452559#M283857</link>
      <description>&lt;P&gt;some more fun:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data one;
input id $3. visit;
datalines;
101 1
101 1
101 .
101 .
101 2
101 .
101 2
101 3
101 .
;
run;

data _null_;
if _n_=1 then do;
if 0 then set one;
 dcl hash h(multidata: 'y', ordered: 'a');
 h.definekey('_n_');
 h.definedata('id','visit');
 h.definedone();
end;
do _n_=nobs to 1 by -1;
set one point=_n_ nobs=nobs;
v=_n_;
if visit and not _visit  then _visit=visit;
else if  visit and  _visit and visit ne _visit then _visit=visit;
else if not visit and _visit then visit=_visit;
else if visit=_visit then call missing(_visit);
h.add();
output;
end;
h.output(dataset:'want');
stop;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 09 Apr 2018 16:53:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/fill-gap-between-records/m-p/452559#M283857</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-04-09T16:53:07Z</dc:date>
    </item>
    <item>
      <title>Re: fill gap between records</title>
      <link>https://communities.sas.com/t5/SAS-Programming/fill-gap-between-records/m-p/452959#M283858</link>
      <description>&lt;P&gt;Perhaps this is less complicated.&amp;nbsp; Read all the records for a given id once to build an array (VIS) of visit values.&amp;nbsp; Each element of the array contains the visit number for the corresponding group of records.&amp;nbsp; Each group is a sequence of identical visit values.&amp;nbsp; For ID 101, there are seven groups with values {1, ., 2, ., 2, 3, .}.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The reread (and output) the same records.&amp;nbsp; If a record has a value of . and is in group g, then check whether groups g-1 and g+1 have identical values.&amp;nbsp; If they do, then fillin in that value.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So if&amp;nbsp;&amp;nbsp; group g has a visit value of . and groups g-1 and g+1 have matching values, then replace.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (drop=_:) ;

  array vis{0:20} _temporary_;

  do _g=1 by 1 until (last.id);
    do until (last.visit);
      set one;
      by id visit notsorted;
    end;
    vis{_g}=visit;
  end;
  _ng=_g;

  do _g=1 to _ng;
    if vis{_g}=. and vis{_g-1}=vis{_g+1} then _fillvis=vis{_g-1};
    else _fillvis=.;
    do until (last.visit);
      set one;
      by id visit notsorted;
      if visit=. then visit=_fillvis;
      output;
    end;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 10 Apr 2018 21:08:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/fill-gap-between-records/m-p/452959#M283858</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2018-04-10T21:08:15Z</dc:date>
    </item>
  </channel>
</rss>

