<?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: How to select the first observation that has a non-missing value among the last N rows? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-select-the-first-observation-that-has-a-non-missing-value/m-p/827430#M326846</link>
    <description>&lt;P&gt;It would help a lot if you explained what the overall goal is because this request seems silly.&lt;/P&gt;
&lt;P&gt;Does your data only have 1's and missing?&amp;nbsp; Or could there be other non-missing values?&amp;nbsp; Does it matter what the first non-missing value is?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Why not just use a temporary array to make a rolling list of the previous N values?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let N=3 ;
data want ;
  set have;
  array previous [&amp;amp;n] _temporary_ ;
  previous[1+mod(_n_,&amp;amp;n)]=value;
  n_previous = n(of previous[*]);
  select = n_previous=1 and not missing(value);
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;PS: Note there is no need to use such convoluted data steps to share such simple data.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input value @@ ;
datalines;
. .
1
. . . .
1
.
1
. . .
1 1
. .
;

data expect ;
  input value select ;
datalines;
. 0
. 0
1 1
. 0
. 0
. 0
. 0
1 1
. 0
1 0
. 0
. 0
. 0
1 1
1 0
. 0
. 0
;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 05 Aug 2022 19:03:47 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2022-08-05T19:03:47Z</dc:date>
    <item>
      <title>How to select the first observation that has a non-missing value among the last N rows?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-select-the-first-observation-that-has-a-non-missing-value/m-p/827416#M326837</link>
      <description>&lt;P&gt;I find operations across rows difficult in SAS. For example, I want to select observations that have a non-missing value, while excluding those for which another observation was already selected among the last N rows. Below is an example for a small N (e.g., N=3), but I want to keep N general.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data have;
 	infile datalines delimiter = "," missover;
	input value;
	datalines;
	.
	.
	1
	.
	.
	.
	.
	1
	.
	1
	.
	.
	.
	1
	1
	.
	.
run;

data want;
 	infile datalines delimiter = "," missover;
	input value select;
	datalines;
	., 0
	., 0
	1, 1
	., 0 
	., 0
	., 0
	., 0
	1, 1
	., 0
	1, 0
	., 0
	., 0
	., 0
	1, 1
	1, 0
	., 0 
	., 0
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>Fri, 05 Aug 2022 18:13:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-select-the-first-observation-that-has-a-non-missing-value/m-p/827416#M326837</guid>
      <dc:creator>xyxu</dc:creator>
      <dc:date>2022-08-05T18:13:44Z</dc:date>
    </item>
    <item>
      <title>Re: How to select the first observation that has a non-missing value among the last N rows?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-select-the-first-observation-that-has-a-non-missing-value/m-p/827426#M326843</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;First, thanks for putting both your sample data and expected results in the form of usable data steps.&amp;nbsp; So here is a program actually tested against your data, and identical to your desired results:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let N=3;
data want (drop=_:);
  set have;

  retain _most_recent_select .; /*Obsnum of most recent select=1*/

  if not missing(value) and _most_recent_select &amp;lt; (_n_-&amp;amp;n) then do;
    select=1;
    _most_recent_select=_n_;
  end;
  else select=0;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Because _most_recent_select is initialized as a missing value, it starts out "less than" all valid numeric values.&amp;nbsp; So the first non-missing VALUE is always selected.&amp;nbsp; After that only obs more than &amp;amp;N after a selected obs is eligible for select=1.&lt;/P&gt;</description>
      <pubDate>Fri, 05 Aug 2022 18:44:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-select-the-first-observation-that-has-a-non-missing-value/m-p/827426#M326843</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2022-08-05T18:44:24Z</dc:date>
    </item>
    <item>
      <title>Re: How to select the first observation that has a non-missing value among the last N rows?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-select-the-first-observation-that-has-a-non-missing-value/m-p/827430#M326846</link>
      <description>&lt;P&gt;It would help a lot if you explained what the overall goal is because this request seems silly.&lt;/P&gt;
&lt;P&gt;Does your data only have 1's and missing?&amp;nbsp; Or could there be other non-missing values?&amp;nbsp; Does it matter what the first non-missing value is?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Why not just use a temporary array to make a rolling list of the previous N values?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let N=3 ;
data want ;
  set have;
  array previous [&amp;amp;n] _temporary_ ;
  previous[1+mod(_n_,&amp;amp;n)]=value;
  n_previous = n(of previous[*]);
  select = n_previous=1 and not missing(value);
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;PS: Note there is no need to use such convoluted data steps to share such simple data.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input value @@ ;
datalines;
. .
1
. . . .
1
.
1
. . .
1 1
. .
;

data expect ;
  input value select ;
datalines;
. 0
. 0
1 1
. 0
. 0
. 0
. 0
1 1
. 0
1 0
. 0
. 0
. 0
1 1
1 0
. 0
. 0
;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 05 Aug 2022 19:03:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-select-the-first-observation-that-has-a-non-missing-value/m-p/827430#M326846</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-08-05T19:03:47Z</dc:date>
    </item>
  </channel>
</rss>

