<?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 missing with the next known values across columns in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Fill-missing-with-the-next-known-values-across-columns/m-p/475327#M122217</link>
    <description>&lt;P&gt;Sorry but I just realized the current code still does not work like what I want for people who work at night.&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;do _n_=1 to dim(t);
if (1&amp;lt;=_n_&amp;lt;=6) or (19&amp;lt;=_n_&amp;lt;=dim(t)) and missing(t(_n_)) then t(_n_)=coalesce(of t(*));
end;
end;&lt;/PRE&gt;&lt;P&gt;For this part, it seems that you assume the end time of work is 5a, and start time of work is 6p. In reality the time of start and end varies between people. It would be perfect if the code can fill the blanks from 0a to the last non-missing 1 in the morning, and fill the blanks from first nonmissing 1 from afternoon to 0a. I tried this but did not work well:&lt;/P&gt;&lt;PRE&gt;do _n_=1 to k;
if missing(t(_n_)) then t(_n_)=coalesce(of t(*));
end;
do _n_= k1+1 to dim(t);
if missing(t(_n_)) then t(_n_)=coalesce(of t(*));
end;
end;&lt;/PRE&gt;&lt;P&gt;Any suggestions?&amp;nbsp; Thanks!!&lt;/P&gt;</description>
    <pubDate>Tue, 03 Jul 2018 21:33:07 GMT</pubDate>
    <dc:creator>panda</dc:creator>
    <dc:date>2018-07-03T21:33:07Z</dc:date>
    <item>
      <title>Fill missing with the next known values across columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Fill-missing-with-the-next-known-values-across-columns/m-p/475270#M122202</link>
      <description>&lt;P&gt;Hi,&amp;nbsp;&lt;/P&gt;&lt;P&gt;My current data looks like this, hour0-hour23 mean the 24 hours across the day. This data tends to record the time of work. Some people started work during daytime and some started work at night. So the start hour is 6a for some and 6p for others.&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
   input id hour0-hour23;
   cards;
1 . . . . . . 1 1 . . 1 1 1 1 . 1 1 1 . . . . . .
2 . . . . . . . . 1 1 1 1 1 . . . 1 1 1 . 1 . . .
3 1 1 . . 1 1 . . . . . . . . . . . . 1 1 1 . . .
4 1 1 1 1 . 1 . . . . . . . . . . 1 1 1 1 . 1 1 1
;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I want to fill the missing values between the columns while people at work, but leave the column as missing at night if people started work in the morning and leave the column as missing during the day if people started work at night. The ideal want data looks like this:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
input id hour0-hour23;
cards;
1 . . . . . . 1 1 1 1 1 1 1 1 1 1 1 1 . . . . . .
2 . . . . . . . . 1 1 1 1 1 1 1 1 1 1 1 1 1 . . .
3 1 1 1 1 1 1 . . . . . . . . . . . . 1 1 1 1 1 1
4 1 1 1 1 1 1 . . . . . . . . . . 1 1 1 1 1 1 1 1
;&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I am not sure how to do that. Any idea? Thanks!!&lt;/P&gt;</description>
      <pubDate>Tue, 03 Jul 2018 17:13:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Fill-missing-with-the-next-known-values-across-columns/m-p/475270#M122202</guid>
      <dc:creator>panda</dc:creator>
      <dc:date>2018-07-03T17:13:57Z</dc:date>
    </item>
    <item>
      <title>Re: Fill missing with the next known values across columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Fill-missing-with-the-next-known-values-across-columns/m-p/475290#M122205</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
   input id hour0-hour23;
   cards;
1 . . . . . . 1 1 . . 1 1 1 1 . 1 1 1 . . . . . .
2 . . . . . . . . 1 1 1 1 1 . . . 1 1 1 . 1 . . .
3 1 1 . . 1 1 . . . . . . . . . . . . 1 1 1 . . .
4 1 1 1 1 . 1 . . . . . . . . . . 1 1 1 1 . 1 1 1
;
data want;
set have;
array t(*)hour0-hour23;
array t1(*) hour23-hour0;
k=whichn(1,of t(*));
k1=dim(t)-whichn(1,of t1(*));
if whichn(1,of t(*))&amp;lt;7 then do;
do _n_=1 to dim(t);
if (1&amp;lt;=_n_&amp;lt;=6) or (19&amp;lt;=_n_&amp;lt;=dim(t)) and missing(t(_n_)) then t(_n_)=coalesce(of t(*));
end;
end;
else if 7&amp;lt;=whichn(1,of t(*))&amp;lt;=18 then do;
do _n_=k to k1;
t(_n_)=coalesce(of t(*));
end;
end;
drop k:;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 03 Jul 2018 18:37:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Fill-missing-with-the-next-known-values-across-columns/m-p/475290#M122205</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-07-03T18:37:46Z</dc:date>
    </item>
    <item>
      <title>Re: Fill missing with the next known values across columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Fill-missing-with-the-next-known-values-across-columns/m-p/475294#M122207</link>
      <description>&lt;P&gt;Thanks&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;! Your code works perfectly. Could you explain what does _n_ mean?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Especially I can't understand the logic of this line of code:&lt;/P&gt;&lt;PRE&gt;if (1&amp;lt;=_n_&amp;lt;=6) or (19&amp;lt;=_n_&amp;lt;=dim(t)) and missing(t(_n_)) then t(_n_)=coalesce(of t(*));&lt;/PRE&gt;&lt;P&gt;Thanks!!&lt;/P&gt;</description>
      <pubDate>Tue, 03 Jul 2018 18:54:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Fill-missing-with-the-next-known-values-across-columns/m-p/475294#M122207</guid>
      <dc:creator>panda</dc:creator>
      <dc:date>2018-07-03T18:54:20Z</dc:date>
    </item>
    <item>
      <title>Re: Fill missing with the next known values across columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Fill-missing-with-the-next-known-values-across-columns/m-p/475305#M122210</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/151080"&gt;@panda&lt;/a&gt;Hang on there is some fix needed which i failed to test effectively. I am gonna fix it properly to make it perfect. I sincerely apologise for some lack of attention to detail. Bear with me. I will be right back with a super clean code . Right now, I am very hungry at 2:20pm. Will message back after lunch&lt;/P&gt;</description>
      <pubDate>Tue, 03 Jul 2018 19:17:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Fill-missing-with-the-next-known-values-across-columns/m-p/475305#M122210</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-07-03T19:17:03Z</dc:date>
    </item>
    <item>
      <title>Re: Fill missing with the next known values across columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Fill-missing-with-the-next-known-values-across-columns/m-p/475314#M122211</link>
      <description>&lt;P&gt;Ok, I think the above should work as I believe I am thinking something that may not be warranted. However, I will stay open for any changes required should you notice any erroneous results. So please feel free to &lt;U&gt;reach out the very moment you come across any issue&lt;/U&gt;.&amp;nbsp;&lt;/P&gt;&lt;P&gt;It's a very interesting question&lt;/P&gt;</description>
      <pubDate>Tue, 03 Jul 2018 19:53:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Fill-missing-with-the-next-known-values-across-columns/m-p/475314#M122211</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-07-03T19:53:50Z</dc:date>
    </item>
    <item>
      <title>Re: Fill missing with the next known values across columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Fill-missing-with-the-next-known-values-across-columns/m-p/475317#M122212</link>
      <description>Thanks! The code worked perfectly for me now. Does _n_ mean the count of column which has the first non-missing value?</description>
      <pubDate>Tue, 03 Jul 2018 20:12:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Fill-missing-with-the-next-known-values-across-columns/m-p/475317#M122212</guid>
      <dc:creator>panda</dc:creator>
      <dc:date>2018-07-03T20:12:06Z</dc:date>
    </item>
    <item>
      <title>Re: Fill missing with the next known values across columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Fill-missing-with-the-next-known-values-across-columns/m-p/475318#M122213</link>
      <description>&lt;P&gt;_n_&amp;nbsp; here is an index variable I am using to loop through the array. _n_ happens to be an automatic variable that is not written to the dataset. So typical lazy blokes like me who use that a lot as I am always lazy to type an extra statement.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 03 Jul 2018 20:14:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Fill-missing-with-the-next-known-values-across-columns/m-p/475318#M122213</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-07-03T20:14:05Z</dc:date>
    </item>
    <item>
      <title>Re: Fill missing with the next known values across columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Fill-missing-with-the-next-known-values-across-columns/m-p/475323#M122214</link>
      <description>That makes sense. Sorry I am really not familiar with the whichn function. Could you elaborate on how you decide the cut off time (number): 7, 6, 19, 18? Many thanks!</description>
      <pubDate>Tue, 03 Jul 2018 20:29:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Fill-missing-with-the-next-known-values-across-columns/m-p/475323#M122214</guid>
      <dc:creator>panda</dc:creator>
      <dc:date>2018-07-03T20:29:07Z</dc:date>
    </item>
    <item>
      <title>Re: Fill missing with the next known values across columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Fill-missing-with-the-next-known-values-across-columns/m-p/475324#M122215</link>
      <description>&lt;P&gt;This description should give an easy intuition-&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;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;/TD&gt;&lt;TD&gt;18&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;pm&lt;/TD&gt;&lt;TD&gt;6&lt;/TD&gt;&lt;TD&gt;18&lt;/TD&gt;&lt;TD&gt;19&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;pm&lt;/TD&gt;&lt;TD&gt;7&lt;/TD&gt;&lt;TD&gt;19&lt;/TD&gt;&lt;TD&gt;20&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;pm&lt;/TD&gt;&lt;TD&gt;8&lt;/TD&gt;&lt;TD&gt;20&lt;/TD&gt;&lt;TD&gt;21&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;pm&lt;/TD&gt;&lt;TD&gt;9&lt;/TD&gt;&lt;TD&gt;21&lt;/TD&gt;&lt;TD&gt;22&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;pm&lt;/TD&gt;&lt;TD&gt;10&lt;/TD&gt;&lt;TD&gt;22&lt;/TD&gt;&lt;TD&gt;23&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;pm&lt;/TD&gt;&lt;TD&gt;11&lt;/TD&gt;&lt;TD&gt;23&lt;/TD&gt;&lt;TD&gt;24&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I will definitely come up with some notes to explain the code and logic a bit later. I am working on my assignment now, so little busy&lt;/P&gt;</description>
      <pubDate>Tue, 03 Jul 2018 20:48:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Fill-missing-with-the-next-known-values-across-columns/m-p/475324#M122215</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-07-03T20:48:58Z</dc:date>
    </item>
    <item>
      <title>Re: Fill missing with the next known values across columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Fill-missing-with-the-next-known-values-across-columns/m-p/475325#M122216</link>
      <description>Got it, thanks! This is very helpful! &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;</description>
      <pubDate>Tue, 03 Jul 2018 20:48:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Fill-missing-with-the-next-known-values-across-columns/m-p/475325#M122216</guid>
      <dc:creator>panda</dc:creator>
      <dc:date>2018-07-03T20:48:32Z</dc:date>
    </item>
    <item>
      <title>Re: Fill missing with the next known values across columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Fill-missing-with-the-next-known-values-across-columns/m-p/475327#M122217</link>
      <description>&lt;P&gt;Sorry but I just realized the current code still does not work like what I want for people who work at night.&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;do _n_=1 to dim(t);
if (1&amp;lt;=_n_&amp;lt;=6) or (19&amp;lt;=_n_&amp;lt;=dim(t)) and missing(t(_n_)) then t(_n_)=coalesce(of t(*));
end;
end;&lt;/PRE&gt;&lt;P&gt;For this part, it seems that you assume the end time of work is 5a, and start time of work is 6p. In reality the time of start and end varies between people. It would be perfect if the code can fill the blanks from 0a to the last non-missing 1 in the morning, and fill the blanks from first nonmissing 1 from afternoon to 0a. I tried this but did not work well:&lt;/P&gt;&lt;PRE&gt;do _n_=1 to k;
if missing(t(_n_)) then t(_n_)=coalesce(of t(*));
end;
do _n_= k1+1 to dim(t);
if missing(t(_n_)) then t(_n_)=coalesce(of t(*));
end;
end;&lt;/PRE&gt;&lt;P&gt;Any suggestions?&amp;nbsp; Thanks!!&lt;/P&gt;</description>
      <pubDate>Tue, 03 Jul 2018 21:33:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Fill-missing-with-the-next-known-values-across-columns/m-p/475327#M122217</guid>
      <dc:creator>panda</dc:creator>
      <dc:date>2018-07-03T21:33:07Z</dc:date>
    </item>
    <item>
      <title>Re: Fill missing with the next known values across columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Fill-missing-with-the-next-known-values-across-columns/m-p/475328#M122218</link>
      <description>&lt;P&gt;My apprehensions has just come true lol. Will be back with the fix., How urgent it is? is it ok, if i give right after my class lectures?&lt;/P&gt;</description>
      <pubDate>Tue, 03 Jul 2018 21:34:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Fill-missing-with-the-next-known-values-across-columns/m-p/475328#M122218</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-07-03T21:34:51Z</dc:date>
    </item>
    <item>
      <title>Re: Fill missing with the next known values across columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Fill-missing-with-the-next-known-values-across-columns/m-p/475329#M122219</link>
      <description>Thanks for the prompt reply! I can definitely wait till you finish your class! Sorry to interrupt your own work for this... &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;</description>
      <pubDate>Tue, 03 Jul 2018 21:38:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Fill-missing-with-the-next-known-values-across-columns/m-p/475329#M122219</guid>
      <dc:creator>panda</dc:creator>
      <dc:date>2018-07-03T21:38:30Z</dc:date>
    </item>
    <item>
      <title>Re: Fill missing with the next known values across columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Fill-missing-with-the-next-known-values-across-columns/m-p/475331#M122220</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/151080"&gt;@panda&lt;/a&gt;&amp;nbsp;Plz try and let me know&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
   input id hour0-hour23;
   cards;
1 . . . . . . 1 1 . . 1 1 1 1 . 1 1 1 . . . . . .
2 . . . . . . . . 1 1 1 1 1 . . . 1 1 1 . 1 . . .
3 1 1 . . 1 1 . . . . . . . . . . . . 1 1 1 . . .
4 1 1 1 1 . 1 . . . . . . . . . . 1 1 1 1 . 1 1 1
;

data want;
set have;
array t(*)hour0-hour23;
array t1(*) hour23-hour0;
array t2(*) hour0-hour5;
array t3(*) hour18-hour23;
k=whichn(1,of t(*));
k1=dim(t)-whichn(1,of t1(*));
k2=whichn(1,of t2(*));
if whichn(1,of t(*))&amp;lt;7 then do;
do _n_=k2 to dim(t2);
if missing(t2(_n_)) then t2(_n_)= coalesce(of t2(*));
end;
do _n_= dim(t3) by -1 to 1;
if missing( t3(_n_)) then t3(_n_)= coalesce(of t3(*));
end;
end;
else if 7&amp;lt;=whichn(1,of t(*))&amp;lt;=18 then do;
do _n_=k to k1;
t(_n_)=coalesce(of t(*));
end;
end;
drop k:;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Also plz test by replacing&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;do _n_= dim(t3) by -1 to 1;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;in the above with&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;&lt;STRONG&gt;do _n_= dim(t3) by -1 to whichn(1,of t3(*));&amp;nbsp;&lt;/STRONG&gt;&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 03 Jul 2018 22:17:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Fill-missing-with-the-next-known-values-across-columns/m-p/475331#M122220</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-07-03T22:17:51Z</dc:date>
    </item>
    <item>
      <title>Re: Fill missing with the next known values across columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Fill-missing-with-the-next-known-values-across-columns/m-p/475338#M122227</link>
      <description>&lt;P&gt;Sorry to bug you again, but instead of assuming hour5 to be the last hour of non-missing value in the morning, and hour18 to be the first hour of non-missing value after 12. Is there a way to detect the position of last non-missing value before 12pm and first non-missing value after 12pm?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If my data is like this:&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
   input id hour0-hour23;
   cards;
1 . . . . . . 1 1 . . 1 1 1 1 . 1 1 1 . . . . . .
2 . . . . . . . . 1 1 1 1 1 . . . 1 1 1 . 1 . . .
3 1 1 . . 1 1 . 1 . . . . . . . . . . 1 1 1 . . .
4 1 1 1 1 . 1 . . . . . . . . . . 1 1 1 1 . 1 1 1
;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Your solution will leave a blank on hour 6 for id 3.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks again!!&lt;/P&gt;</description>
      <pubDate>Tue, 03 Jul 2018 23:36:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Fill-missing-with-the-next-known-values-across-columns/m-p/475338#M122227</guid>
      <dc:creator>panda</dc:creator>
      <dc:date>2018-07-03T23:36:17Z</dc:date>
    </item>
    <item>
      <title>Re: Fill missing with the next known values across columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Fill-missing-with-the-next-known-values-across-columns/m-p/475353#M122233</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/151080"&gt;@panda&lt;/a&gt;&amp;nbsp;No need for words like sorry in a knowledge sharing forum. Please feel free to post any number of questions/answers etc.&lt;/P&gt;&lt;P&gt;OK, try the modified based on your latest description--&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
   input id hour0-hour23;
   cards;
1 . . . . . . 1 1 . . 1 1 1 1 . 1 1 1 . . . . . .
2 . . . . . . . . 1 1 1 1 1 . . . 1 1 1 . 1 . . .
3 1 1 . . 1 1 . 1 . . . . . . . . . . 1 1 1 . . .
4 1 1 1 1 . 1 . . . . . . . . . . 1 1 1 1 . 1 1 1
;

data want;
set have;
array t(*)hour0-hour23;
array t1(*) hour23-hour0;
k=whichn(1,of t(*));
k1=dim(t)-whichn(1,of t1(*));
if whichn(1,of t(*))&amp;lt;7 then do;
do _n_=12 by -1 to 1; /*check before 12*/
if not missing(t(_n_)) then k2=1;
else if missing(t(_n_)) and k2 then t(_n_)= coalesce(of t(*));
end;
k2=0;
do _n_= 14 to dim(t); /*check after 12*/
if not missing(t(_n_)) then k2=1;
else if missing(t(_n_)) and k2 then t(_n_)= coalesce(of t(*));
end;
end;
else if 7&amp;lt;=whichn(1,of t(*))&amp;lt;=18 then do;
do _n_=k to k1;
t(_n_)=coalesce(of t(*));
end;
end;
drop k:;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 04 Jul 2018 04:06:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Fill-missing-with-the-next-known-values-across-columns/m-p/475353#M122233</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-07-04T04:06:09Z</dc:date>
    </item>
    <item>
      <title>Re: Fill missing with the next known values across columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Fill-missing-with-the-next-known-values-across-columns/m-p/475472#M122277</link>
      <description>&lt;P&gt;Thanks&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;! This finally works exactly as I want !&lt;/P&gt;</description>
      <pubDate>Wed, 04 Jul 2018 17:22:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Fill-missing-with-the-next-known-values-across-columns/m-p/475472#M122277</guid>
      <dc:creator>panda</dc:creator>
      <dc:date>2018-07-04T17:22:42Z</dc:date>
    </item>
  </channel>
</rss>

