<?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: getting the first non-missing value in a column in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/getting-the-first-non-missing-value-in-a-column/m-p/848032#M335286</link>
    <description>&lt;P&gt;I wanted not missing location but all the date and outcome data have to come from the most recent date. I am interested in the data with most recent date and backfilling location. But I found the solution if anyone comes across this...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data fake_data;
input patID $ date monyy6. location $ outcome ;
format date monyy.;
datalines;
1693 Dec-14 . 1
1693 Nov-14 . 1
1693 Oct-14 CA 0
1693 Sep-14 CA 0
1693 Aug-14 CO 0
1693 Jul-14 CO 0
1129 Feb-18 . 1 
1129 Jan-18 . 0
1129 Dec-17 NY 0
1129 Nov-17 PA 0
1345 Jun-19 . 0 
1345 May-19 . 0 
1345 Apr-19 NY 0 
1345 Mar-19 NY 0
1345 Feb-19 NY 0
1345 Jan-19 CA 0 
1345 Dec-18 CA 0
1345 Nov-18 CA 0
;

proc sort data=fake_data; by patID descending date; run;
data fake_row;
  set fake_data;
  rownum=_n_;
run;

proc sql;
create table test as 
select A.*, B.rownum as rownum2,
coalesce(A.location, B.location) AS location_2
from fake_row A, fake_row B
where A.patID = B.patID
group by A.patID
order by A.patID, A.date desc;
quit;

data test2;
	set test;
	where not missing(location_2);
	row = rownum + rownum2;
run;
proc sort data=test2; by patID row; run;

data final;
	set test2;
	by patID row;
	if first.patID;
run;&lt;/PRE&gt;</description>
    <pubDate>Tue, 06 Dec 2022 06:15:53 GMT</pubDate>
    <dc:creator>daufoi</dc:creator>
    <dc:date>2022-12-06T06:15:53Z</dc:date>
    <item>
      <title>getting the first non-missing value in a column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/getting-the-first-non-missing-value-in-a-column/m-p/848029#M335283</link>
      <description>&lt;P&gt;I have the following data. I sorted it by ID and date. How can I get the first date for each patient but if there is a missing value in the location column, I want the next non-missing value?&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data fake_data;&lt;BR /&gt;input patID $ date monyy6. location $ outcome ;&lt;BR /&gt;format date monyy.;&lt;BR /&gt;datalines;&lt;BR /&gt;1693 Dec-14 . 1&lt;BR /&gt;1693 Nov-14 . 1&lt;BR /&gt;1693 Oct-14 CA 1&lt;BR /&gt;1693 Sep-14 CA 0&lt;BR /&gt;1693 Aug-14 CO 0&lt;BR /&gt;1693 Jul-14 CO 0&lt;BR /&gt;1129 Feb-18 . 1 &lt;BR /&gt;1129 Jan-18 . 0&lt;BR /&gt;1129 Dec-17 NY 0&lt;BR /&gt;1129 Nov-17 PA 0&lt;BR /&gt;1345 Jun-19 . 0 &lt;BR /&gt;1345 May-19 . 0 &lt;BR /&gt;1345 Apr-19 NY 0 &lt;BR /&gt;1345 Mar-19 NY 0&lt;BR /&gt;1345 Feb-19 NY 0&lt;BR /&gt;1345 Jan-19 CA 0 &lt;BR /&gt;1345 Dec-18 CA 0&lt;BR /&gt;1345 Nov-18 CA 0&lt;BR /&gt;;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;P&gt;I have this so far...&lt;CODE class=""&gt;&lt;/CODE&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;proc sql;
create table test as 
select A.*, 
coalesce(A.location, B.location) AS location_2
from fake_data A, fake_data B
where A.patID = B.patID
group by A.patID
order by A.patID, A.date desc;
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I would like to be able to sort it so that I can choose the first entry which will have all columns with data.&lt;/P&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 06 Dec 2022 06:06:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/getting-the-first-non-missing-value-in-a-column/m-p/848029#M335283</guid>
      <dc:creator>daufoi</dc:creator>
      <dc:date>2022-12-06T06:06:38Z</dc:date>
    </item>
    <item>
      <title>Re: getting the first non-missing value in a column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/getting-the-first-non-missing-value-in-a-column/m-p/848031#M335285</link>
      <description>&lt;P&gt;It is not clear what you expect as result.&lt;/P&gt;
&lt;P&gt;If you just want the first obs with non-missing location for each id, then try:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set fake_data;
  by patID notsorted; /* EDIT: Your data is grouped  by id, but not sorted */
  where not missing(location);
  if first.patID;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 06 Dec 2022 06:03:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/getting-the-first-non-missing-value-in-a-column/m-p/848031#M335285</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2022-12-06T06:03:32Z</dc:date>
    </item>
    <item>
      <title>Re: getting the first non-missing value in a column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/getting-the-first-non-missing-value-in-a-column/m-p/848032#M335286</link>
      <description>&lt;P&gt;I wanted not missing location but all the date and outcome data have to come from the most recent date. I am interested in the data with most recent date and backfilling location. But I found the solution if anyone comes across this...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data fake_data;
input patID $ date monyy6. location $ outcome ;
format date monyy.;
datalines;
1693 Dec-14 . 1
1693 Nov-14 . 1
1693 Oct-14 CA 0
1693 Sep-14 CA 0
1693 Aug-14 CO 0
1693 Jul-14 CO 0
1129 Feb-18 . 1 
1129 Jan-18 . 0
1129 Dec-17 NY 0
1129 Nov-17 PA 0
1345 Jun-19 . 0 
1345 May-19 . 0 
1345 Apr-19 NY 0 
1345 Mar-19 NY 0
1345 Feb-19 NY 0
1345 Jan-19 CA 0 
1345 Dec-18 CA 0
1345 Nov-18 CA 0
;

proc sort data=fake_data; by patID descending date; run;
data fake_row;
  set fake_data;
  rownum=_n_;
run;

proc sql;
create table test as 
select A.*, B.rownum as rownum2,
coalesce(A.location, B.location) AS location_2
from fake_row A, fake_row B
where A.patID = B.patID
group by A.patID
order by A.patID, A.date desc;
quit;

data test2;
	set test;
	where not missing(location_2);
	row = rownum + rownum2;
run;
proc sort data=test2; by patID row; run;

data final;
	set test2;
	by patID row;
	if first.patID;
run;&lt;/PRE&gt;</description>
      <pubDate>Tue, 06 Dec 2022 06:15:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/getting-the-first-non-missing-value-in-a-column/m-p/848032#M335286</guid>
      <dc:creator>daufoi</dc:creator>
      <dc:date>2022-12-06T06:15:53Z</dc:date>
    </item>
  </channel>
</rss>

