<?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: Find first non-missing value and shift data to the left in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Find-first-non-missing-value-and-shift-data-to-the-left/m-p/273341#M54464</link>
    <description>&lt;P&gt;This won't work if you have skips - ie proc1 has a value, proc2 is missing, proc3 has a value.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Otherwise this is simple brute force method to move the values using an array.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile cards truncover;
input Patient_ID $     Proc1    Proc2    Proc3    Proc4;
cards;
12345 42.1 56.2 22.1 33.4
23456 42.1 56.2 22.1 33.4
34567 .      .  22.1 33.4
;
run;

data want;
set have;
array proc(4) proc1-proc4;

do i=1 to 4;
	if proc(i) ne .  then leave;
end;

*move values to missing records;
if i ne 1 then do j=1 to dim(proc)-i+1;
	proc(j)=proc(i);
	proc(i)=.;
	i=i+1;
end;

drop i j;

run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 26 May 2016 18:02:36 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2016-05-26T18:02:36Z</dc:date>
    <item>
      <title>Find first non-missing value and shift data to the left</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-first-non-missing-value-and-shift-data-to-the-left/m-p/273325#M54461</link>
      <description>&lt;P&gt;I have a data set which contains patients and the list of their procedures, but some of a patient's first procedure codes are missing. &amp;nbsp;Is there a way to shift the data over?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So the data looks like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Patient ID &amp;nbsp; &amp;nbsp; &amp;nbsp;Proc1 &amp;nbsp; &amp;nbsp;Proc2 &amp;nbsp; &amp;nbsp;Proc3 &amp;nbsp; &amp;nbsp;Proc4&lt;/P&gt;&lt;P&gt;12345 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 42.1 &amp;nbsp; &amp;nbsp; &amp;nbsp; 56.2 &amp;nbsp; &amp;nbsp; &amp;nbsp;22.1 &amp;nbsp; &amp;nbsp; &amp;nbsp;33.4&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;23456&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;42.1 &amp;nbsp; &amp;nbsp; &amp;nbsp; 56.2 &amp;nbsp; &amp;nbsp; &amp;nbsp;22.1 &amp;nbsp; &amp;nbsp; &amp;nbsp;33.4&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;34567&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;. &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;.&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;22.1 &amp;nbsp; &amp;nbsp; &amp;nbsp;33.4&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;And I want the data for the last patient Id to have their procedure codes shift over so that the first procedure codes are not missing:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Patient ID &amp;nbsp; &amp;nbsp; &amp;nbsp;Proc1 &amp;nbsp; &amp;nbsp;Proc2 &amp;nbsp; &amp;nbsp;Proc3 &amp;nbsp; &amp;nbsp;Proc4&lt;/P&gt;&lt;P&gt;12345 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 42.1 &amp;nbsp; &amp;nbsp; &amp;nbsp; 56.2 &amp;nbsp; &amp;nbsp; &amp;nbsp;22.1 &amp;nbsp; &amp;nbsp; &amp;nbsp;33.4&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;23456&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;42.1 &amp;nbsp; &amp;nbsp; &amp;nbsp; 56.2 &amp;nbsp; &amp;nbsp; &amp;nbsp;22.1 &amp;nbsp; &amp;nbsp; &amp;nbsp;33.4&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;34567 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;22.1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;33.4&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Does anyone know how to do this?&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Thanks!&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 26 May 2016 17:20:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-first-non-missing-value-and-shift-data-to-the-left/m-p/273325#M54461</guid>
      <dc:creator>einstein</dc:creator>
      <dc:date>2016-05-26T17:20:22Z</dc:date>
    </item>
    <item>
      <title>Re: Find first non-missing value and shift data to the left</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-first-non-missing-value-and-shift-data-to-the-left/m-p/273329#M54462</link>
      <description>&lt;P&gt;Is there any additional logic that needs to be considered?&lt;/P&gt;
&lt;P&gt;Does it need to be a straight reassignment, ie proc3 to proc1 or can proc4 go to proc1?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can you have all missing values?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 26 May 2016 17:33:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-first-non-missing-value-and-shift-data-to-the-left/m-p/273329#M54462</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-05-26T17:33:18Z</dc:date>
    </item>
    <item>
      <title>Re: Find first non-missing value and shift data to the left</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-first-non-missing-value-and-shift-data-to-the-left/m-p/273333#M54463</link>
      <description>&lt;P&gt;Yes, whatever the first non-missing value is should be reassigned to the first missing value. &amp;nbsp;So yes, if proc1-proc3 are missing, but proc4-proc6 are not then proc4 becomes proc1 and proc5 becomes proc2, etc.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;And yes, there are patients with all missing values in which case nothing happens to them.&lt;/P&gt;</description>
      <pubDate>Thu, 26 May 2016 17:40:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-first-non-missing-value-and-shift-data-to-the-left/m-p/273333#M54463</guid>
      <dc:creator>einstein</dc:creator>
      <dc:date>2016-05-26T17:40:04Z</dc:date>
    </item>
    <item>
      <title>Re: Find first non-missing value and shift data to the left</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-first-non-missing-value-and-shift-data-to-the-left/m-p/273341#M54464</link>
      <description>&lt;P&gt;This won't work if you have skips - ie proc1 has a value, proc2 is missing, proc3 has a value.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Otherwise this is simple brute force method to move the values using an array.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile cards truncover;
input Patient_ID $     Proc1    Proc2    Proc3    Proc4;
cards;
12345 42.1 56.2 22.1 33.4
23456 42.1 56.2 22.1 33.4
34567 .      .  22.1 33.4
;
run;

data want;
set have;
array proc(4) proc1-proc4;

do i=1 to 4;
	if proc(i) ne .  then leave;
end;

*move values to missing records;
if i ne 1 then do j=1 to dim(proc)-i+1;
	proc(j)=proc(i);
	proc(i)=.;
	i=i+1;
end;

drop i j;

run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 26 May 2016 18:02:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-first-non-missing-value-and-shift-data-to-the-left/m-p/273341#M54464</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-05-26T18:02:36Z</dc:date>
    </item>
    <item>
      <title>Re: Find first non-missing value and shift data to the left</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-first-non-missing-value-and-shift-data-to-the-left/m-p/273351#M54467</link>
      <description>&lt;P&gt;Are your variables character or numeric?&amp;nbsp; (It's not going to be that difficult, but it's easier to do once than twice.)&lt;/P&gt;</description>
      <pubDate>Thu, 26 May 2016 18:34:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-first-non-missing-value-and-shift-data-to-the-left/m-p/273351#M54467</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2016-05-26T18:34:02Z</dc:date>
    </item>
    <item>
      <title>Re: Find first non-missing value and shift data to the left</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-first-non-missing-value-and-shift-data-to-the-left/m-p/273355#M54469</link>
      <description>&lt;P&gt;Variables are character.&lt;/P&gt;</description>
      <pubDate>Thu, 26 May 2016 18:51:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-first-non-missing-value-and-shift-data-to-the-left/m-p/273355#M54469</guid>
      <dc:creator>einstein</dc:creator>
      <dc:date>2016-05-26T18:51:47Z</dc:date>
    </item>
    <item>
      <title>Re: Find first non-missing value and shift data to the left</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-first-non-missing-value-and-shift-data-to-the-left/m-p/273356#M54470</link>
      <description>&lt;P&gt;Ah! &amp;nbsp;This worked! &amp;nbsp;Thanks!&lt;/P&gt;</description>
      <pubDate>Thu, 26 May 2016 18:53:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-first-non-missing-value-and-shift-data-to-the-left/m-p/273356#M54470</guid>
      <dc:creator>einstein</dc:creator>
      <dc:date>2016-05-26T18:53:18Z</dc:date>
    </item>
    <item>
      <title>Re: Find first non-missing value and shift data to the left</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-first-non-missing-value-and-shift-data-to-the-left/m-p/273376#M54476</link>
      <description>&lt;P&gt;It's good that PROC&lt;EM&gt;i&lt;/EM&gt; are character variables because this avoids numeric representation issues. I assume that you adapted Reeza's code correspondingly (i.e. used&amp;nbsp;&lt;FONT face="courier new,courier"&gt;' '&lt;/FONT&gt; in place of &lt;STRONG&gt;&lt;FONT face="courier new,courier"&gt;.&lt;/FONT&gt;&lt;/STRONG&gt; to denote missing values) so as to avoid type conversion messages in the log and values such as &lt;FONT face="courier new,courier"&gt;' &amp;nbsp; &amp;nbsp; &amp;nbsp; .'&lt;/FONT&gt; instead of blank values.&lt;/P&gt;</description>
      <pubDate>Thu, 26 May 2016 19:37:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-first-non-missing-value-and-shift-data-to-the-left/m-p/273376#M54476</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2016-05-26T19:37:01Z</dc:date>
    </item>
    <item>
      <title>Re: Find first non-missing value and shift data to the left</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-first-non-missing-value-and-shift-data-to-the-left/m-p/273473#M54505</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile cards truncover;
input Patient_ID $     Proc1    Proc2    Proc3    Proc4;
cards;
12345 42.1 56.2 22.1 33.4
23456 42.1 56.2 22.1 33.4
34567 .      .  22.1 33.4
;
run;
data want;
 set have;
 array p{*} proc1-proc4;
 array x{*} new1-new4;
 n=0;
 do i=1 to dim(p);
  if not missing(p{i}) then do;
   n+1;x{n}=p{i};
  end;
 end;
 do i=1 to dim(p);
  p{i}=x{i};
 end;
 drop i n new1-new4;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 27 May 2016 03:17:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-first-non-missing-value-and-shift-data-to-the-left/m-p/273473#M54505</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-05-27T03:17:03Z</dc:date>
    </item>
  </channel>
</rss>

