<?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 Selecting the first occurrence based on the value of a variable. in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Selecting-the-first-occurrence-based-on-the-value-of-a-variable/m-p/594792#M170975</link>
    <description>&lt;P&gt;I want to select the first instance where the var1 value is &amp;gt;= 0.5;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Help please&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data have;&lt;BR /&gt;input id visitid pageid date var1 start_date;&lt;BR /&gt;datalines;&lt;BR /&gt;1 1 1 10/1/2019 0.1 9/30/2019&lt;BR /&gt;1 1 2 10/2/2019 0.2 9/30/2019&lt;BR /&gt;1 1 2 10/3/2019 0.5 9/30/2019&lt;BR /&gt;1 2 1 10/4/2019 1 9/30/2019&lt;BR /&gt;1 2 2 10/5/2019 3 9/30/2019&lt;BR /&gt;1 2 3 10/6/2019 5 9/30/2019&lt;BR /&gt;2 1 1 9/28/2019 0.1 9/27/2019&lt;BR /&gt;2 1 2 9/29/2019 0.2 9/27/2019&lt;BR /&gt;2 1 2 9/29/2019 0.5 9/27/2019&lt;BR /&gt;2 1 3 10/4/2019 1 9/27/2019&lt;BR /&gt;2 2 1 10/5/2019 3 9/27/2019&lt;BR /&gt;2 2 2 10/6/2019 5 9/27/2019&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;data want;&lt;BR /&gt;input id visitid pageid date var1 start_date;&lt;BR /&gt;datalines;&lt;BR /&gt;1 1 2 10/3/2019 0.5 9/30/2019&lt;BR /&gt;2 1 2 9/29/2019 0.5 9/27/2019&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 08 Oct 2019 16:04:28 GMT</pubDate>
    <dc:creator>GopiV</dc:creator>
    <dc:date>2019-10-08T16:04:28Z</dc:date>
    <item>
      <title>Selecting the first occurrence based on the value of a variable.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Selecting-the-first-occurrence-based-on-the-value-of-a-variable/m-p/594792#M170975</link>
      <description>&lt;P&gt;I want to select the first instance where the var1 value is &amp;gt;= 0.5;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Help please&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data have;&lt;BR /&gt;input id visitid pageid date var1 start_date;&lt;BR /&gt;datalines;&lt;BR /&gt;1 1 1 10/1/2019 0.1 9/30/2019&lt;BR /&gt;1 1 2 10/2/2019 0.2 9/30/2019&lt;BR /&gt;1 1 2 10/3/2019 0.5 9/30/2019&lt;BR /&gt;1 2 1 10/4/2019 1 9/30/2019&lt;BR /&gt;1 2 2 10/5/2019 3 9/30/2019&lt;BR /&gt;1 2 3 10/6/2019 5 9/30/2019&lt;BR /&gt;2 1 1 9/28/2019 0.1 9/27/2019&lt;BR /&gt;2 1 2 9/29/2019 0.2 9/27/2019&lt;BR /&gt;2 1 2 9/29/2019 0.5 9/27/2019&lt;BR /&gt;2 1 3 10/4/2019 1 9/27/2019&lt;BR /&gt;2 2 1 10/5/2019 3 9/27/2019&lt;BR /&gt;2 2 2 10/6/2019 5 9/27/2019&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;data want;&lt;BR /&gt;input id visitid pageid date var1 start_date;&lt;BR /&gt;datalines;&lt;BR /&gt;1 1 2 10/3/2019 0.5 9/30/2019&lt;BR /&gt;2 1 2 9/29/2019 0.5 9/27/2019&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 08 Oct 2019 16:04:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Selecting-the-first-occurrence-based-on-the-value-of-a-variable/m-p/594792#M170975</guid>
      <dc:creator>GopiV</dc:creator>
      <dc:date>2019-10-08T16:04:28Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting the first occurrence based on the value of a variable.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Selecting-the-first-occurrence-based-on-the-value-of-a-variable/m-p/594797#M170976</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
by id;
retain flag;
if first.id then flag = 0;
if not flag and var1 &amp;gt;= 0.5
then do;
  flag = 1;
  output;
end;
drop flag;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Untested, as I'm on my tablet.&lt;/P&gt;</description>
      <pubDate>Tue, 08 Oct 2019 16:15:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Selecting-the-first-occurrence-based-on-the-value-of-a-variable/m-p/594797#M170976</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-10-08T16:15:16Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting the first occurrence based on the value of a variable.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Selecting-the-first-occurrence-based-on-the-value-of-a-variable/m-p/594798#M170977</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id visitid pageid date :mmddyy10. var1 start_date :mmddyy10.;
format date start_date mmddyy10.;
datalines;
1 1 1 10/1/2019 0.1 9/30/2019
1 1 2 10/2/2019 0.2 9/30/2019
1 1 2 10/3/2019 0.5 9/30/2019
1 2 1 10/4/2019 1 9/30/2019
1 2 2 10/5/2019 3 9/30/2019
1 2 3 10/6/2019 5 9/30/2019
2 1 1 9/28/2019 0.1 9/27/2019
2 1 2 9/29/2019 0.2 9/27/2019
2 1 2 9/29/2019 0.5 9/27/2019
2 1 3 10/4/2019 1 9/27/2019
2 2 1 10/5/2019 3 9/27/2019
2 2 2 10/6/2019 5 9/27/2019
;
run;

data want;
 do _n_=0 by 0 until(last.id);
  set have;
  by id;
  if var1&amp;gt;= 0.5 and _n_=0 then do;
  output ;
  _n_=1;
  end;
 end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 08 Oct 2019 16:16:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Selecting-the-first-occurrence-based-on-the-value-of-a-variable/m-p/594798#M170977</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-10-08T16:16:25Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting the first occurrence based on the value of a variable.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Selecting-the-first-occurrence-based-on-the-value-of-a-variable/m-p/594801#M170979</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/294253"&gt;@GopiV&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Maybe you can try this :&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=have out=have2 (where=(var1 &amp;gt;= 0.5));
	by id var1;
run;

data want;
	set have2;
	by id var1;
	if first.id then output;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Best regards,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 08 Oct 2019 16:24:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Selecting-the-first-occurrence-based-on-the-value-of-a-variable/m-p/594801#M170979</guid>
      <dc:creator>ed_sas_member</dc:creator>
      <dc:date>2019-10-08T16:24:07Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting the first occurrence based on the value of a variable.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Selecting-the-first-occurrence-based-on-the-value-of-a-variable/m-p/594804#M170982</link>
      <description>&lt;P&gt;Very nice&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/292097"&gt;@ed_sas_member&lt;/a&gt;&amp;nbsp; &amp;nbsp;If one can assume, all Var1 values will be either &amp;gt;=0.5 following the 1st occurance, and if that holds true the where filter is fine, also I am not sure whether a SORT is needed beforehand considering OP's dataset seems to be already sorted by ID and DATE.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So a sequential where , going with assumption can be without the SORT i would think?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
	set have(where=(var1 &amp;gt;= 0.5));
	by id ;
	if first.id ;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 08 Oct 2019 16:32:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Selecting-the-first-occurrence-based-on-the-value-of-a-variable/m-p/594804#M170982</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-10-08T16:32:45Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting the first occurrence based on the value of a variable.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Selecting-the-first-occurrence-based-on-the-value-of-a-variable/m-p/594841#M171009</link>
      <description>&lt;P&gt;If the data is in proper order by ID:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   set have;
   by id;
   where var1 &amp;gt;= 0.5;
   if first.id;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The interaction of the BY and WHERE statements is important.&amp;nbsp; WHERE sets up first.id and last.id based on only the observations that pass the WHERE filter.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 08 Oct 2019 18:16:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Selecting-the-first-occurrence-based-on-the-value-of-a-variable/m-p/594841#M171009</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2019-10-08T18:16:45Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting the first occurrence based on the value of a variable.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Selecting-the-first-occurrence-based-on-the-value-of-a-variable/m-p/594868#M171020</link>
      <description>&lt;P&gt;Thank you&lt;/P&gt;</description>
      <pubDate>Tue, 08 Oct 2019 19:06:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Selecting-the-first-occurrence-based-on-the-value-of-a-variable/m-p/594868#M171020</guid>
      <dc:creator>GopiV</dc:creator>
      <dc:date>2019-10-08T19:06:38Z</dc:date>
    </item>
  </channel>
</rss>

