<?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: Using the First.with multiple BY variables, find first occurance for unique combo of by variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Using-the-First-with-multiple-BY-variables-find-first-occurance/m-p/366097#M87044</link>
    <description>&lt;P&gt;Post test data in the form of a datastep!!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;/* assumes sorted */

data want;
  set have;
  by id1 id2;
  if first.id2 then new_var=1;
run;&lt;/PRE&gt;</description>
    <pubDate>Mon, 12 Jun 2017 10:28:53 GMT</pubDate>
    <dc:creator>RW9</dc:creator>
    <dc:date>2017-06-12T10:28:53Z</dc:date>
    <item>
      <title>Using the First.with multiple BY variables, find first occurance for unique combo of by variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-the-First-with-multiple-BY-variables-find-first-occurance/m-p/366094#M87041</link>
      <description>&lt;P&gt;How can i get first occurance basis multiple by variable for eg&lt;/P&gt;&lt;P&gt;for below data i need 1 in new column where ever i have unique combination of id1 &amp;amp; id2&lt;/P&gt;&lt;TABLE cellspacing="0" cellpadding="0" border="0"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;id1&lt;/TD&gt;&lt;TD&gt;id2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1001&lt;/TD&gt;&lt;TD&gt;10&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1001&lt;/TD&gt;&lt;TD&gt;10&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1001&lt;/TD&gt;&lt;TD&gt;11&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1001&lt;/TD&gt;&lt;TD&gt;10&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1002&lt;/TD&gt;&lt;TD&gt;12&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1002&lt;/TD&gt;&lt;TD&gt;12&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1002&lt;/TD&gt;&lt;TD&gt;13&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
      <pubDate>Mon, 12 Jun 2017 10:20:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-the-First-with-multiple-BY-variables-find-first-occurance/m-p/366094#M87041</guid>
      <dc:creator>MohitDamani</dc:creator>
      <dc:date>2017-06-12T10:20:34Z</dc:date>
    </item>
    <item>
      <title>Re: Using the First.with multiple BY variables, find first occurance for unique combo of by variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-the-First-with-multiple-BY-variables-find-first-occurance/m-p/366095#M87042</link>
      <description>&lt;P&gt;Please show which of your example observations should be flagged.&lt;/P&gt;
&lt;P&gt;Do you need to preserve the current order?&lt;/P&gt;</description>
      <pubDate>Mon, 12 Jun 2017 10:24:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-the-First-with-multiple-BY-variables-find-first-occurance/m-p/366095#M87042</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-06-12T10:24:05Z</dc:date>
    </item>
    <item>
      <title>Re: Using the First.with multiple BY variables, find first occurance for unique combo of by variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-the-First-with-multiple-BY-variables-find-first-occurance/m-p/366097#M87044</link>
      <description>&lt;P&gt;Post test data in the form of a datastep!!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;/* assumes sorted */

data want;
  set have;
  by id1 id2;
  if first.id2 then new_var=1;
run;&lt;/PRE&gt;</description>
      <pubDate>Mon, 12 Jun 2017 10:28:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-the-First-with-multiple-BY-variables-find-first-occurance/m-p/366097#M87044</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-06-12T10:28:53Z</dc:date>
    </item>
    <item>
      <title>Re: Using the First.with multiple BY variables, find first occurance for unique combo of by variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-the-First-with-multiple-BY-variables-find-first-occurance/m-p/366098#M87045</link>
      <description>&lt;P&gt;I think this is what you want, but please post your data in the form of a datastep and describe your desired outcome if not&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id1 id2;
datalines;
1001 10
1001 10
1001 11
1001 10
1002 12
1002 12
1002 13
;

proc sort data = have;
	by id1 id2;
run;

data want;
	set have;
	by id1 id2;
	if first.id2 then first_unique = 1;
	else first_unique = 0;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 12 Jun 2017 10:39:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-the-First-with-multiple-BY-variables-find-first-occurance/m-p/366098#M87045</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2017-06-12T10:39:50Z</dc:date>
    </item>
    <item>
      <title>Re: Using the First.with multiple BY variables, find first occurance for unique combo of by variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-the-First-with-multiple-BY-variables-find-first-occurance/m-p/366101#M87048</link>
      <description>&lt;P&gt;data have;&lt;BR /&gt;input id1 id2;&lt;BR /&gt;datalines;&lt;BR /&gt;1001 10&lt;BR /&gt;1001 10&lt;BR /&gt;1001 11&lt;BR /&gt;1001 10&lt;BR /&gt;1002 12&lt;BR /&gt;1002 12&lt;BR /&gt;1002 13&lt;BR /&gt;;&lt;BR /&gt;RUN;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sort data = have;&lt;BR /&gt; by id1 id2;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;BR /&gt; set have;&lt;BR /&gt; by id1 id2;&lt;BR /&gt; if first.id1 then first_unique = 1;&lt;BR /&gt; else first_unique = 0;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;OR&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;if yo want to extract the unique data then pls use below code ;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;proc sort data = have OUT= WANT NODUP;BY ID1;&lt;BR /&gt; &lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Mon, 12 Jun 2017 11:04:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-the-First-with-multiple-BY-variables-find-first-occurance/m-p/366101#M87048</guid>
      <dc:creator>TarunKumar</dc:creator>
      <dc:date>2017-06-12T11:04:16Z</dc:date>
    </item>
    <item>
      <title>Re: Using the First.with multiple BY variables, find first occurance for unique combo of by variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-the-First-with-multiple-BY-variables-find-first-occurance/m-p/366117#M87053</link>
      <description>&lt;P&gt;I am not sure of your expected output, you want the unique records per id1 and id2 without the duplicates I mean if there are a combination of dulicates on id1 and id2 then exclude them from flagging&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 id1 id2;
if first.id2 and last.id2 then flag=1;
else flag=0;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 12 Jun 2017 12:24:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-the-First-with-multiple-BY-variables-find-first-occurance/m-p/366117#M87053</guid>
      <dc:creator>Jagadishkatam</dc:creator>
      <dc:date>2017-06-12T12:24:43Z</dc:date>
    </item>
    <item>
      <title>Re: Using the First.with multiple BY variables, find first occurance for unique combo of by variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-the-First-with-multiple-BY-variables-find-first-occurance/m-p/366172#M87084</link>
      <description>Tried, not working, i need combination of both variables like we do partition by in SQL</description>
      <pubDate>Mon, 12 Jun 2017 14:51:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-the-First-with-multiple-BY-variables-find-first-occurance/m-p/366172#M87084</guid>
      <dc:creator>MohitDamani</dc:creator>
      <dc:date>2017-06-12T14:51:20Z</dc:date>
    </item>
    <item>
      <title>Re: Using the First.with multiple BY variables, find first occurance for unique combo of by variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-the-First-with-multiple-BY-variables-find-first-occurance/m-p/366175#M87087</link>
      <description>i need combination of both variables like we do partition by in SQL, for eg row 1,3,5 &amp;amp; 7 should have 1 rest 0</description>
      <pubDate>Mon, 12 Jun 2017 14:53:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-the-First-with-multiple-BY-variables-find-first-occurance/m-p/366175#M87087</guid>
      <dc:creator>MohitDamani</dc:creator>
      <dc:date>2017-06-12T14:53:30Z</dc:date>
    </item>
    <item>
      <title>Re: Using the First.with multiple BY variables, find first occurance for unique combo of by variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-the-First-with-multiple-BY-variables-find-first-occurance/m-p/366179#M87090</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/148436"&gt;@MohitDamani&lt;/a&gt; wrote:&lt;BR /&gt;i need combination of both variables like we do partition by in SQL, for eg row 1,3,5 &amp;amp; 7 should have 1 rest 0&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;BR /&gt;A slight expansion of &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31304"&gt;@PeterClemmensen&lt;/a&gt;'s code shows that it clearly works:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id1 id2;
n = _n_;
datalines;
1001 10
1001 10
1001 11
1001 10
1002 12
1002 12
1002 13
;
run;

proc sort data = have;
	by id1 id2;
run;

data want;
	set have;
	by id1 id2;
	if first.id2 then first_unique = 1;
	else first_unique = 0;
run;

proc print data=want noobs;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;                     first_
  id1    id2    n    unique

 1001     10    1       1  
 1001     10    2       0  
 1001     10    4       0  
 1001     11    3       1  
 1002     12    5       1  
 1002     12    6       0  
 1002     13    7       1  
&lt;/PRE&gt;
&lt;P&gt;If you need the original order restored, just sort by n.&lt;/P&gt;</description>
      <pubDate>Mon, 12 Jun 2017 15:03:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-the-First-with-multiple-BY-variables-find-first-occurance/m-p/366179#M87090</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-06-12T15:03:56Z</dc:date>
    </item>
  </channel>
</rss>

