<?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: Select unique obs when a variable first meets a condition and is less than other condition. in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Select-unique-obs-when-a-variable-first-meets-a-condition-and-is/m-p/911889#M359540</link>
    <description>&lt;P&gt;Below should work for you&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* option 1 */
proc sort data=esfe2visitestrus out=inter;
  by giltid ageontest avgwtkg;
run; 
data want_1;
  set inter;
  by giltid ageontest avgwtkg;
  retain selected_flg;
  if first.giltid then selected_flg=0;
  if selected_flg=0 and avgwtkg&amp;gt;=136 then
    do;
      output;
      selected_flg=1;
    end;
  drop selected_flg;
run;

/* option 2 */
proc sql;
  create table want_2 as
  select *
  from esfe2visitestrus
  where avgwtkg&amp;gt;=136
  group by giltid
  having min(ageontest)=ageontest
  ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 18 Jan 2024 00:13:52 GMT</pubDate>
    <dc:creator>Patrick</dc:creator>
    <dc:date>2024-01-18T00:13:52Z</dc:date>
    <item>
      <title>Select unique obs when a variable first meets a condition and is less than other condition.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-unique-obs-when-a-variable-first-meets-a-condition-and-is/m-p/911859#M359532</link>
      <description>&lt;P&gt;Hello all.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am running a pig study and am trying to get SAS to output a dataset which requires the following:&lt;/P&gt;&lt;P&gt;the first time each animal reaches 136 kg before 200 d of age. I could just go with the 200 d mark, however, with weight fluctuation, an animal may meet the criteria x number of days before. I have attached a datastep of the data with each of the codes that I have tried. I have tried using the data step, proc sql, and proc summary, to no avail. I can get a list of all the times each animal (giltid) reaches the critera(avgwtkg&amp;gt;=136 and ageontest&amp;lt;=200), but I want something similar to the first. utilized in the data step.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am looking for results which include all variables in the main set, but the key info would be:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Giltid:223307 avgwtkg:136.321 ageontest:193&lt;/P&gt;&lt;P&gt;Giltid:223308 avgwtkg:136.02 ageontest: 188&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;and so on...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data esfe2200dwt;&lt;BR /&gt;set esfe2visitestrus;&lt;BR /&gt;by giltid avgwtkg;&lt;BR /&gt;first.avgwtkg=avgwtkg;&lt;BR /&gt;if (ageontest&amp;lt;=200) and (first.avgwtkg&amp;gt;=136) then output;&lt;BR /&gt;run;&lt;BR /&gt;proc sql ;&lt;BR /&gt;select distinct(giltid) as giltid, avgwtkg, ageontest&lt;BR /&gt;from esfe2visitestrus&lt;BR /&gt;distinct where avgwtkg&amp;gt;=136 and ageontest&amp;lt;=200;&lt;BR /&gt;proc summary data=esfe2visitestrus nway;&lt;BR /&gt;class giltid feedlevel pg600 entrydate;&lt;BR /&gt;where avgwtkg&amp;gt;=136 and ageontest&amp;lt;=200;&lt;BR /&gt;id _all_;&lt;BR /&gt;output out=test200dwt ;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for your time and consideration.&lt;/P&gt;</description>
      <pubDate>Wed, 17 Jan 2024 19:51:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-unique-obs-when-a-variable-first-meets-a-condition-and-is/m-p/911859#M359532</guid>
      <dc:creator>rtniblett15</dc:creator>
      <dc:date>2024-01-17T19:51:06Z</dc:date>
    </item>
    <item>
      <title>Re: Select unique obs when a variable first meets a condition and is less than other condition.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-unique-obs-when-a-variable-first-meets-a-condition-and-is/m-p/911889#M359540</link>
      <description>&lt;P&gt;Below should work for you&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* option 1 */
proc sort data=esfe2visitestrus out=inter;
  by giltid ageontest avgwtkg;
run; 
data want_1;
  set inter;
  by giltid ageontest avgwtkg;
  retain selected_flg;
  if first.giltid then selected_flg=0;
  if selected_flg=0 and avgwtkg&amp;gt;=136 then
    do;
      output;
      selected_flg=1;
    end;
  drop selected_flg;
run;

/* option 2 */
proc sql;
  create table want_2 as
  select *
  from esfe2visitestrus
  where avgwtkg&amp;gt;=136
  group by giltid
  having min(ageontest)=ageontest
  ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 18 Jan 2024 00:13:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-unique-obs-when-a-variable-first-meets-a-condition-and-is/m-p/911889#M359540</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2024-01-18T00:13:52Z</dc:date>
    </item>
    <item>
      <title>Re: Select unique obs when a variable first meets a condition and is less than other condition.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-unique-obs-when-a-variable-first-meets-a-condition-and-is/m-p/912499#M359708</link>
      <description>&lt;P&gt;Thank you! Option 1 has one obs that is over 200 d of age. Option 2 still has every gilt obs with a weight over 136 kg like before.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 22 Jan 2024 16:44:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-unique-obs-when-a-variable-first-meets-a-condition-and-is/m-p/912499#M359708</guid>
      <dc:creator>rtniblett15</dc:creator>
      <dc:date>2024-01-22T16:44:59Z</dc:date>
    </item>
  </channel>
</rss>

