<?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: Populating All Missing Variables With the Right Most Non-Missing Value in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Populating-All-Missing-Variables-With-the-Right-Most-Non-Missing/m-p/465899#M118846</link>
    <description>&lt;P&gt;Thanks again&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/76464"&gt;@s_lassen&lt;/a&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I appreciate the rigor and additional consideration you have given my problem.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 30 May 2018 05:27:05 GMT</pubDate>
    <dc:creator>Scott_Mitchell</dc:creator>
    <dc:date>2018-05-30T05:27:05Z</dc:date>
    <item>
      <title>Populating All Missing Variables With the Right Most Non-Missing Value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Populating-All-Missing-Variables-With-the-Right-Most-Non-Missing/m-p/464977#M118569</link>
      <description>&lt;P&gt;Good Evening All,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a hierarchy&amp;nbsp;which has holes in it as per the HAVE dataset code below.&amp;nbsp; I am looking to first maintain the right most populated value whilst filling all blanks to the left (the code attached with the Array does this nicely).&amp;nbsp; Then I need to find the right most populated record and hold it's value until ORG_LVL_10.&amp;nbsp; I am struggling with the&amp;nbsp;finding a solution that performs the latter task.&amp;nbsp; Any help would be greatly appreciated.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you for all your help.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Regards,&lt;/P&gt;
&lt;P&gt;Scott&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data have;&lt;BR /&gt; length org_lvl_1 org_lvl_2 org_lvl_3 org_lvl_4 org_lvl_5 org_lvl_6 org_lvl_7 org_lvl_8 org_lvl_9 org_lvl_10 $20.;&lt;BR /&gt; infile datalines dlm= ',' dsd missover;&lt;BR /&gt; input org_lvl_1 org_lvl_2 org_lvl_3 org_lvl_4 org_lvl_5 org_lvl_6 org_lvl_7 org_lvl_8 org_lvl_9 org_lvl_10;&lt;BR /&gt;&lt;BR /&gt; datalines;&lt;BR /&gt;LEVEL1,LEVEL2,LEVEL3,LEVEL4,LEVEL5,LEVEL6,LEVEL7,LEVEL8,LEVEL9,LEVEL10&lt;BR /&gt;LEVEL1,LEVEL2,LEVEL3,LEVEL4,LEVEL5,&lt;BR /&gt;LEVEL1,LEVEL2,LEVEL3,LEVEL4,,LEVEL6,LEVEL7,LEVEL8,LEVEL9,LEVEL10&lt;BR /&gt;LEVEL1,LEVEL2,LEVEL3,LEVEL4,,LEVEL6,LEVEL7,,,LEVEL10&lt;BR /&gt;;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;data want;&lt;BR /&gt; length org_lvl_1 org_lvl_2 org_lvl_3 org_lvl_4 org_lvl_5 org_lvl_6 org_lvl_7 org_lvl_8 org_lvl_9 org_lvl_10 $20.;&lt;BR /&gt; infile datalines dlm= ',' dsd missover;&lt;BR /&gt; input org_lvl_1 org_lvl_2 org_lvl_3 org_lvl_4 org_lvl_5 org_lvl_6 org_lvl_7 org_lvl_8 org_lvl_9 org_lvl_10;&lt;BR /&gt;&lt;BR /&gt; datalines;&lt;BR /&gt;LEVEL1,LEVEL2,LEVEL3,LEVEL4,LEVEL5,LEVEL6,LEVEL7,LEVEL8,LEVEL9,LEVEL10&lt;BR /&gt;LEVEL1,LEVEL2,LEVEL3,LEVEL4,LEVEL5,LEVEL5,LEVEL5,LEVEL5,LEVEL5,LEVEL5,&lt;BR /&gt;LEVEL1,LEVEL2,LEVEL3,LEVEL4,LEVEL6,LEVEL6,LEVEL7,LEVEL8,LEVEL9,LEVEL10&lt;BR /&gt;LEVEL1,LEVEL2,LEVEL3,LEVEL4,LEVEL6,LEVEL6,LEVEL7,LEVEL10,LEVEL10,LEVEL10&lt;BR /&gt;;&lt;BR /&gt;run;&lt;BR /&gt; &lt;BR /&gt;&lt;BR /&gt;DATA test;
	SET	have;
	ARRAY _ORG $ ORG_LVL_1 - ORG_LVL_10;
		J=1;
		DO I=1 TO DIM(_ORG);
			PUT I = J = _ORG(I) = _ORG(J) =;
			IF TRIM(LEFT(_ORG(I))) NE "" THEN DO;
				_ORG(J) = COALESCEC(TRIM(LEFT(_ORG(J))),TRIM(LEFT(_ORG(I))));
				J+1;
			END;
		END;
RUN;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 25 May 2018 07:56:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Populating-All-Missing-Variables-With-the-Right-Most-Non-Missing/m-p/464977#M118569</guid>
      <dc:creator>Scott_Mitchell</dc:creator>
      <dc:date>2018-05-25T07:56:12Z</dc:date>
    </item>
    <item>
      <title>Re: Populating All Missing Variables With the Right Most Non-Missing Value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Populating-All-Missing-Variables-With-the-Right-Most-Non-Missing/m-p/464991#M118571</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/52595"&gt;@Scott_Mitchell&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;Below should do the job:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
 length org_lvl_1 org_lvl_2 org_lvl_3 org_lvl_4 org_lvl_5 org_lvl_6 org_lvl_7 org_lvl_8 org_lvl_9 org_lvl_10 $20.;
 infile datalines dlm= ',' dsd missover;
 input org_lvl_1 org_lvl_2 org_lvl_3 org_lvl_4 org_lvl_5 org_lvl_6 org_lvl_7 org_lvl_8 org_lvl_9 org_lvl_10;

 datalines;
LEVEL1,LEVEL2,LEVEL3,LEVEL4,LEVEL5,LEVEL6,LEVEL7,LEVEL8,LEVEL9,LEVEL10
LEVEL1,LEVEL2,LEVEL3,LEVEL4,LEVEL5,
LEVEL1,LEVEL2,LEVEL3,LEVEL4,,LEVEL6,LEVEL7,LEVEL8,LEVEL9,LEVEL10
LEVEL1,LEVEL2,LEVEL3,LEVEL4,,LEVEL6,LEVEL7,,,LEVEL10
;
run;

data want;
  set have;
  array org_lvl {*} org_lvl_:;
  do _i=1 to dim(org_lvl);
    if missing(org_lvl[_i]) then
      do _j=_i+1 to dim(org_lvl) until(not missing(org_lvl[_i]));
        org_lvl[_i]=org_lvl[_j];
      end;
      if missing(org_lvl[_i]) then org_lvl[_i]=org_lvl[max(_i-1,1)];
  end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 25 May 2018 08:44:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Populating-All-Missing-Variables-With-the-Right-Most-Non-Missing/m-p/464991#M118571</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2018-05-25T08:44:29Z</dc:date>
    </item>
    <item>
      <title>Re: Populating All Missing Variables With the Right Most Non-Missing Value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Populating-All-Missing-Variables-With-the-Right-Most-Non-Missing/m-p/465020#M118583</link>
      <description>&lt;P&gt;Here is a relatively simple program which does the trick:&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;data want;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; set have;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; array org_lvl {*} org_lvl_:;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; /* if last element is missing, set it to last non-missing */&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; if missing(org_lvl(dim(org_lvl))) then do;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; &amp;nbsp; do _N_=dim(org_lvl)-1 to 1 by -1 while(missing(org_lvl(_N_)));&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; end;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; &amp;nbsp; org_lvl(dim(org_lvl))=org_lvl(_N_);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; &amp;nbsp; end;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; /* fill left when missing */&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; do _N_=dim(org_lvl)-1 to 1 by -1;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; &amp;nbsp; if missing(org_lvl(_N_)) then&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; org_lvl(_N_)=org_lvl(_N_+1);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; &amp;nbsp; end;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;run;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Not only is it quite easy to understand, it also performs well (with 400,000 obs, my version took about 0.26 CPU seconds, while your original version took 0.78 seconds - the solution suggested by&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12447"&gt;@Patrick&lt;/a&gt; was in between).&lt;/P&gt;</description>
      <pubDate>Fri, 25 May 2018 12:07:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Populating-All-Missing-Variables-With-the-Right-Most-Non-Missing/m-p/465020#M118583</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2018-05-25T12:07:06Z</dc:date>
    </item>
    <item>
      <title>Re: Populating All Missing Variables With the Right Most Non-Missing Value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Populating-All-Missing-Variables-With-the-Right-Most-Non-Missing/m-p/465391#M118706</link>
      <description>&lt;P&gt;Thank you both for your help.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This worked perfectly.&lt;/P&gt;</description>
      <pubDate>Mon, 28 May 2018 03:54:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Populating-All-Missing-Variables-With-the-Right-Most-Non-Missing/m-p/465391#M118706</guid>
      <dc:creator>Scott_Mitchell</dc:creator>
      <dc:date>2018-05-28T03:54:09Z</dc:date>
    </item>
    <item>
      <title>Re: Populating All Missing Variables With the Right Most Non-Missing Value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Populating-All-Missing-Variables-With-the-Right-Most-Non-Missing/m-p/465418#M118711</link>
      <description>&lt;P&gt;I have been thinking a bit - if all values are missing, you will get an index-out-of-range error, as _N_=0 after the first loop.&lt;/P&gt;&lt;P&gt;So you may want to put in a safeguard:&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;data want;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; set have;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; array org_lvl {*} org_lvl_:;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; /* if last element is missing, set it to last non-missing */&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; if missing(org_lvl(dim(org_lvl))) then do;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; &amp;nbsp; do _N_=dim(org_lvl)-1 to 1 by -1 while(missing(org_lvl(_N_)));&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; end;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; &amp;nbsp; if _N_=0 then /* in case all values are missing */&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; return;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; &amp;nbsp; org_lvl(dim(org_lvl))=org_lvl(_N_);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; &amp;nbsp; end;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; /* fill left when missing */&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; do _N_=dim(org_lvl)-1 to 1 by -1;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; &amp;nbsp; if missing(org_lvl(_N_)) then&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; org_lvl(_N_)=org_lvl(_N_+1);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; &amp;nbsp; end;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;run;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="arial,helvetica,sans-serif"&gt;I did it with a RETURN statement, as the second loop will not do anything useful in that case.&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 28 May 2018 07:58:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Populating-All-Missing-Variables-With-the-Right-Most-Non-Missing/m-p/465418#M118711</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2018-05-28T07:58:48Z</dc:date>
    </item>
    <item>
      <title>Re: Populating All Missing Variables With the Right Most Non-Missing Value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Populating-All-Missing-Variables-With-the-Right-Most-Non-Missing/m-p/465899#M118846</link>
      <description>&lt;P&gt;Thanks again&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/76464"&gt;@s_lassen&lt;/a&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I appreciate the rigor and additional consideration you have given my problem.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 30 May 2018 05:27:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Populating-All-Missing-Variables-With-the-Right-Most-Non-Missing/m-p/465899#M118846</guid>
      <dc:creator>Scott_Mitchell</dc:creator>
      <dc:date>2018-05-30T05:27:05Z</dc:date>
    </item>
  </channel>
</rss>

