<?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: choosing the first row satisfying another column in a long file in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/choosing-the-first-row-satisfying-another-column-in-a-long-file/m-p/726965#M225994</link>
    <description>&lt;P&gt;If you insist on SQL, you will have to go through a tedious process. This is because there is no efficient function to get the first one.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input Patient Oxygen $ Tier Hourfromadmission;
datalines;
1 NC 1 3
1 HF 3 6
1 V  3 12
2 CPAP 3 2
;
run;

proc sql;
  create table tmp as
  select patient, oxygen, MONOTONIC() as ID from have
  where tier=3
  ;
  select oxygen from tmp as tmp1
  where tmp1.id=(select min(id) as id2 from tmp group by patient having tmp1.id=tmp.id)
  ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;BR /&gt;For data steps, it is easy to use by and first.&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 patient;
  where tier=3;
  if first.patient;
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>Wed, 17 Mar 2021 03:00:54 GMT</pubDate>
    <dc:creator>japelin</dc:creator>
    <dc:date>2021-03-17T03:00:54Z</dc:date>
    <item>
      <title>choosing the first row satisfying another column in a long file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/choosing-the-first-row-satisfying-another-column-in-a-long-file/m-p/726959#M225988</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a long file of oxygen which contains many timestamped rows of oxygen per patient.&amp;nbsp; I'm trying to generate a proc sql statement that chooses the "Oxygen" value where the Tier column reaches 3 for the first time. I've already sorted the data. In this case it would be "HF" for patient 1 and "CPAP" for patient 2. If anyone has code that might work, would love to use it. Thanks!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Patient&amp;nbsp; Oxygen&amp;nbsp; Tier&amp;nbsp; &amp;nbsp;Hour from admission&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; NC&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&amp;nbsp; &amp;nbsp; &amp;nbsp; 3&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; HF&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3&amp;nbsp; &amp;nbsp; &amp;nbsp; 6&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; V&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;12&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; CPAP&amp;nbsp; &amp;nbsp; &amp;nbsp; 3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2&lt;/P&gt;</description>
      <pubDate>Wed, 17 Mar 2021 01:50:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/choosing-the-first-row-satisfying-another-column-in-a-long-file/m-p/726959#M225988</guid>
      <dc:creator>lmyers2</dc:creator>
      <dc:date>2021-03-17T01:50:02Z</dc:date>
    </item>
    <item>
      <title>Re: choosing the first row satisfying another column in a long file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/choosing-the-first-row-satisfying-another-column-in-a-long-file/m-p/726963#M225992</link>
      <description>&lt;P&gt;SQL really doesn't have a concept of internal data order.&lt;/P&gt;
&lt;P&gt;A data step would be a better choice in general if you want to process data in a specific sequence.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data example;
  input Patient $  Oxygen $  Tier    Hour;
datalines;
1            NC          1      3
1            HF           3      6
1            V             3       12
2            CPAP      3       2
;

data want;
   set example;
   by patient;
   retain firsttime;
   if first.patient then firsttime=0;
   if firsttime=0 and tier ge 3 then do;
      output;
      firsttime=1;
   end;
   drop firsttime;
run;&lt;/PRE&gt;
&lt;P&gt;BY group processing creates special temporary variables that indicate whether a record is the first or last of the group. These are referenced using First.variablename or Last.variablename. The values are numeric 1/0 which SAS treats as true or false and so can be used in IF statements as shown.&lt;/P&gt;
&lt;P&gt;Retain keeps the value of a variable from one iteration of the data step to the next. So we see a flag variable to 0 on the first record of each patient. Then when we get to the tier greater than or equal to 3 it outputs the record and sets the flag so that following records can tell they are not the first time.&lt;/P&gt;
&lt;P&gt;You didn't provide much example data about such things as what if the Tier is never exactly 3? So I assumed that if the value is greater than 3 but 3 was skipped for some reason you want to know.&lt;/P&gt;
&lt;P&gt;The above assumes the data is sorted by patient and an appropriate variable for 'time'.&lt;/P&gt;</description>
      <pubDate>Wed, 17 Mar 2021 02:23:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/choosing-the-first-row-satisfying-another-column-in-a-long-file/m-p/726963#M225992</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-03-17T02:23:47Z</dc:date>
    </item>
    <item>
      <title>Re: choosing the first row satisfying another column in a long file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/choosing-the-first-row-satisfying-another-column-in-a-long-file/m-p/726965#M225994</link>
      <description>&lt;P&gt;If you insist on SQL, you will have to go through a tedious process. This is because there is no efficient function to get the first one.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input Patient Oxygen $ Tier Hourfromadmission;
datalines;
1 NC 1 3
1 HF 3 6
1 V  3 12
2 CPAP 3 2
;
run;

proc sql;
  create table tmp as
  select patient, oxygen, MONOTONIC() as ID from have
  where tier=3
  ;
  select oxygen from tmp as tmp1
  where tmp1.id=(select min(id) as id2 from tmp group by patient having tmp1.id=tmp.id)
  ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;BR /&gt;For data steps, it is easy to use by and first.&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 patient;
  where tier=3;
  if first.patient;
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>Wed, 17 Mar 2021 03:00:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/choosing-the-first-row-satisfying-another-column-in-a-long-file/m-p/726965#M225994</guid>
      <dc:creator>japelin</dc:creator>
      <dc:date>2021-03-17T03:00:54Z</dc:date>
    </item>
    <item>
      <title>Re: choosing the first row satisfying another column in a long file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/choosing-the-first-row-satisfying-another-column-in-a-long-file/m-p/726979#M226005</link>
      <description>&lt;P&gt;Thanks, that simple code&amp;nbsp;worked! &amp;nbsp;As a follow up question, how would you use the data step to find a specific value after a certain criteria is&amp;nbsp;met. I have a long file&amp;nbsp;that has timestamps that is sorted (sample below). I'm looking to find how many patients&amp;nbsp;get "CPAP" &lt;U&gt;any time after&lt;/U&gt; a row of HF. The answer here would be patient 1 and 2 so 2 of the 3 patients.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Patient&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Tier&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; oxygen&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Timestamp&lt;/P&gt;&lt;P&gt;1&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; 1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; NC&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; Time&lt;/P&gt;&lt;P&gt;1&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;3&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;HF&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; Time&lt;/P&gt;&lt;P&gt;1&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; 3&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; CPAP&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Time&lt;/P&gt;&lt;P&gt;2&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; 3&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; HF&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;Time&lt;/P&gt;&lt;P&gt;2&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; 3&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;CPAP&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Time&lt;/P&gt;&lt;P&gt;3&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; 1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; NC&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; Time&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 17 Mar 2021 05:14:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/choosing-the-first-row-satisfying-another-column-in-a-long-file/m-p/726979#M226005</guid>
      <dc:creator>lmyers2</dc:creator>
      <dc:date>2021-03-17T05:14:38Z</dc:date>
    </item>
    <item>
      <title>Re: choosing the first row satisfying another column in a long file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/choosing-the-first-row-satisfying-another-column-in-a-long-file/m-p/726981#M226007</link>
      <description>&lt;P&gt;If you have another question, please create another Topic.&lt;BR /&gt;Otherwise, later visitors will not know which answer to which question is the Solution.&lt;/P&gt;</description>
      <pubDate>Wed, 17 Mar 2021 05:35:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/choosing-the-first-row-satisfying-another-column-in-a-long-file/m-p/726981#M226007</guid>
      <dc:creator>japelin</dc:creator>
      <dc:date>2021-03-17T05:35:22Z</dc:date>
    </item>
  </channel>
</rss>

