<?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 first variable according to a combinations of other variables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Selecting-first-variable-according-to-a-combinations-of-other/m-p/554655#M154328</link>
    <description>&lt;P&gt;Hi Guys,&lt;/P&gt;&lt;P&gt;I need to select the first date of a 2 variables combinations.&lt;/P&gt;&lt;P&gt;For example:&lt;/P&gt;&lt;P&gt;data:&lt;/P&gt;&lt;P&gt;number | pet | date&lt;/P&gt;&lt;P&gt;1 dog&amp;nbsp; 12.11.2018&lt;/P&gt;&lt;P&gt;1 dog 18.6.2019&lt;/P&gt;&lt;P&gt;1 cat 1.1.2016&lt;/P&gt;&lt;P&gt;1 cat 6.3.2015&lt;/P&gt;&lt;P&gt;1 fish&amp;nbsp; 2.2.2016&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;result wanted:&lt;/P&gt;&lt;P&gt;1 dog&amp;nbsp; 12.11.2018&lt;/P&gt;&lt;P&gt;1 cat 6.3.2015&lt;/P&gt;&lt;P&gt;1 fish&amp;nbsp; 2.2.2016&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please help,&lt;/P&gt;&lt;P&gt;Thank you!&lt;/P&gt;</description>
    <pubDate>Mon, 29 Apr 2019 08:58:15 GMT</pubDate>
    <dc:creator>efi88</dc:creator>
    <dc:date>2019-04-29T08:58:15Z</dc:date>
    <item>
      <title>Selecting first variable according to a combinations of other variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Selecting-first-variable-according-to-a-combinations-of-other/m-p/554655#M154328</link>
      <description>&lt;P&gt;Hi Guys,&lt;/P&gt;&lt;P&gt;I need to select the first date of a 2 variables combinations.&lt;/P&gt;&lt;P&gt;For example:&lt;/P&gt;&lt;P&gt;data:&lt;/P&gt;&lt;P&gt;number | pet | date&lt;/P&gt;&lt;P&gt;1 dog&amp;nbsp; 12.11.2018&lt;/P&gt;&lt;P&gt;1 dog 18.6.2019&lt;/P&gt;&lt;P&gt;1 cat 1.1.2016&lt;/P&gt;&lt;P&gt;1 cat 6.3.2015&lt;/P&gt;&lt;P&gt;1 fish&amp;nbsp; 2.2.2016&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;result wanted:&lt;/P&gt;&lt;P&gt;1 dog&amp;nbsp; 12.11.2018&lt;/P&gt;&lt;P&gt;1 cat 6.3.2015&lt;/P&gt;&lt;P&gt;1 fish&amp;nbsp; 2.2.2016&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please help,&lt;/P&gt;&lt;P&gt;Thank you!&lt;/P&gt;</description>
      <pubDate>Mon, 29 Apr 2019 08:58:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Selecting-first-variable-according-to-a-combinations-of-other/m-p/554655#M154328</guid>
      <dc:creator>efi88</dc:creator>
      <dc:date>2019-04-29T08:58:15Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting first variable according to a combinations of other variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Selecting-first-variable-according-to-a-combinations-of-other/m-p/554656#M154329</link>
      <description>&lt;P&gt;Like this?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input number pet $ date:ddmmyy10.;
format date ddmmyy10.;
datalines;
1 dog  12.11.2018
1 dog 18.6.2019
1 cat 1.1.2016
1 cat 6.3.2015
1 fish  2.2.2016
;

proc sql;
   create table want as
   select * from have
   group by number, pet
   having date=min(date);
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 29 Apr 2019 09:07:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Selecting-first-variable-according-to-a-combinations-of-other/m-p/554656#M154329</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2019-04-29T09:07:10Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting first variable according to a combinations of other variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Selecting-first-variable-according-to-a-combinations-of-other/m-p/554657#M154330</link>
      <description>&lt;P&gt;Thanks a lot!&lt;/P&gt;</description>
      <pubDate>Mon, 29 Apr 2019 09:15:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Selecting-first-variable-according-to-a-combinations-of-other/m-p/554657#M154330</guid>
      <dc:creator>efi88</dc:creator>
      <dc:date>2019-04-29T09:15:09Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting first variable according to a combinations of other variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Selecting-first-variable-according-to-a-combinations-of-other/m-p/554667#M154333</link>
      <description>&lt;P&gt;Alternatively by data step&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=have;
by number pet date;
run;

data want;
set have;
by number pet date;
if first.pet;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 29 Apr 2019 11:11:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Selecting-first-variable-according-to-a-combinations-of-other/m-p/554667#M154333</guid>
      <dc:creator>Jagadishkatam</dc:creator>
      <dc:date>2019-04-29T11:11:54Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting first variable according to a combinations of other variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Selecting-first-variable-according-to-a-combinations-of-other/m-p/554758#M154361</link>
      <description>&lt;P&gt;This appears a nice question that can appreciate&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;&amp;nbsp; 's diligence that eloquently entailed reasoning to various approaches &lt;STRONG&gt;FOR&lt;/STRONG&gt; and &lt;STRONG&gt;AGAINST i.e remerge vs not to remerge&amp;nbsp;&lt;/STRONG&gt;in general in a thread ages ago&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1. Need for a HAVING aka filter remerged content vs &lt;STRONG&gt;Direct summary&amp;nbsp;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I suppose that's excellent&amp;nbsp; forward thinking of&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31304"&gt;@PeterClemmensen&lt;/a&gt;&amp;nbsp; to envision the possibility of many variables besides grouping variable that would trigger a remerge and the need for HAVING.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Reeza covers this so well as she explains the double pass of SQL , Datastep that makes the inquisitive minded folks to think further.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Case 1. We have just grouping variable and analysis variable&lt;/P&gt;
&lt;P&gt;Probable solutions: Proc sql direct summary, proc summary, means etc&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;proc sql;
create table want as
select number,pet, min(date) as date format=ddmmyy10.
from have
group by number,pet;
quit;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Case 2. Grouping variable, analysis variables and other variables&lt;/P&gt;
&lt;P&gt;Solutions: warrants HAVING coz&amp;nbsp; the other will trigger remerge that requires grouped filter&lt;/P&gt;
&lt;PRE&gt;proc sql;
   create table want as
   select * from have
   group by number, pet
   having date=min(date);
quit;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Moving on to Datastep:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The Case1 would probably not require a sort assuming already sorted in some order as sample suggests we could just&lt;/P&gt;
&lt;PRE&gt;data want ;
do until(last.pet);
set have(rename=date=_date);
by number pet notsorted;
date=min(date,_date);
end;
drop _:;
format date ddmmyy10.;
run;&lt;/PRE&gt;
&lt;P&gt;However, again in Case 2 with many other vars will involve some gymnastics to park the associated variables of the min date somewhere and bring them back to output.&amp;nbsp; Of course, this can be circumvented with various techniques without gymnastics with a double DOW/Interleave with SET and BY/ Parking in temp array/ Hash etc.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Nonetheless, the sorted approach by&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12151"&gt;@Jagadishkatam&lt;/a&gt;&amp;nbsp; is neat, convenient and easy to maintain.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Overall Long vs wide processing is the essence of the discussion. Cheers!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 29 Apr 2019 16:11:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Selecting-first-variable-according-to-a-combinations-of-other/m-p/554758#M154361</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-04-29T16:11:06Z</dc:date>
    </item>
  </channel>
</rss>

