<?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: Trying to use nodupkey but not sure its the right fit for this situation.  Please help! in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Trying-to-use-nodupkey-but-not-sure-its-the-right-fit-for-this/m-p/558704#M155947</link>
    <description>&lt;P&gt;Unfortunately this doesn't lend itself well to FIRST/LAST though it can probably be done.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's a SQL approach that works, I kept it simple but you can make it more efficient/less steps if needed.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have; 
infile cards dlm=',' truncover dsd;
input ContractID UpLoadID Row;
cards;
1, 1, 1
1, 1, 2
1, 1, 3
1, 1, 4
2, 1, 1
3, 1, 1
3, 1, 2
1, 2, 1
1, 2, 2
1, 2, 3
1, 2, 4
1, 2, 5
2, 2, 1
3, 2, 1
1, 3, 1
1, 3, 3
1, 3, 5
;

proc sql;
create table temp as
select contractID, max(uploadID) as latest_upload
from have
group by contractID;
quit;

proc sql;
create table want as
select t2.*
from temp as t1
inner join have as t2
on t1.contractID=t2.contractID and t1.latest_upload = t2.uploadID
order by 1,2,3;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/274118"&gt;@sirspeedyx0&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Unfortunately we don't have a reliable upload timestamp.&amp;nbsp; We hoped that the Upload ID would replace that, as it counts up each time we upload a new batch of contracts.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 14 May 2019 17:22:12 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2019-05-14T17:22:12Z</dc:date>
    <item>
      <title>Trying to use nodupkey but not sure its the right fit for this situation.  Please help!</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Trying-to-use-nodupkey-but-not-sure-its-the-right-fit-for-this/m-p/558696#M155939</link>
      <description>&lt;P&gt;I am working on a dataset to track quantitative info related to contracts for a company I work for.&amp;nbsp; Each contract can have up to a dozen or so rows of data assigned to it, and we use a unique contract ID to be able to identify an individual contract.&amp;nbsp; However, occasionally we make a mistake when uploading the data, so we also assign sort of a consecutive upload ID so that we know that a contract has been uploaded more than once.&amp;nbsp; Where it gets a little tricky is that the number of rows associated with the contract might change in between uploads.&amp;nbsp; Here is an example:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Contract ID; Upload ID; Row&lt;/P&gt;&lt;P&gt;1, 1, 1&lt;/P&gt;&lt;P&gt;1, 1, 2&lt;/P&gt;&lt;P&gt;1, 1, 3&lt;/P&gt;&lt;P&gt;1, 1, 4&lt;/P&gt;&lt;P&gt;2, 1, 1&lt;/P&gt;&lt;P&gt;3, 1, 1&lt;/P&gt;&lt;P&gt;3, 1, 2&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So here we have completed an upload of 3 contracts.&amp;nbsp; Later on characteristics of the contracts have changed, so we have to do another upload, and then maybe we need yet another revision of one particular contract so we do a 3rd upload.&amp;nbsp; Now the dataset looks like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Contract ID; Upload ID; Row&lt;/P&gt;&lt;P&gt;1, 1, 1&lt;/P&gt;&lt;P&gt;1, 1, 2&lt;/P&gt;&lt;P&gt;1, 1, 3&lt;/P&gt;&lt;P&gt;1, 1, 4&lt;/P&gt;&lt;P&gt;2, 1, 1&lt;/P&gt;&lt;P&gt;3, 1, 1&lt;/P&gt;&lt;P&gt;3, 1, 2&lt;/P&gt;&lt;P&gt;1, 2, 1&lt;/P&gt;&lt;P&gt;1, 2, 2&lt;/P&gt;&lt;P&gt;1, 2, 3&lt;/P&gt;&lt;P&gt;1, 2, 4&lt;/P&gt;&lt;P&gt;1, 2, 5&lt;/P&gt;&lt;P&gt;2, 2, 1&lt;/P&gt;&lt;P&gt;3, 2, 1&lt;/P&gt;&lt;P&gt;1, 3, 1&lt;/P&gt;&lt;P&gt;1, 3, 3&lt;/P&gt;&lt;P&gt;1, 3, 5&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So the question is, is there a function that will pull me all rows related to a specific contract that are from the latest upload?&amp;nbsp; So ultimately I'd want to pull this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Contract ID; Upload ID; Row&lt;/P&gt;&lt;P&gt;2, 2, 1&lt;/P&gt;&lt;P&gt;3, 2, 1&lt;/P&gt;&lt;P&gt;1, 3, 1&lt;/P&gt;&lt;P&gt;1, 3, 3&lt;/P&gt;&lt;P&gt;1, 3, 5&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Many thanks in advance for your help.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 14 May 2019 16:53:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Trying-to-use-nodupkey-but-not-sure-its-the-right-fit-for-this/m-p/558696#M155939</guid>
      <dc:creator>sirspeedyx0</dc:creator>
      <dc:date>2019-05-14T16:53:01Z</dc:date>
    </item>
    <item>
      <title>Re: Trying to use nodupkey but not sure its the right fit for this situation.  Please help!</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Trying-to-use-nodupkey-but-not-sure-its-the-right-fit-for-this/m-p/558699#M155942</link>
      <description>Usually you'll have a date in that type of data as well indicating the load date or something along those lines. Do you have a variable that you're using for that? If so, this is trivial. If not, this is still doable, just more work.</description>
      <pubDate>Tue, 14 May 2019 16:57:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Trying-to-use-nodupkey-but-not-sure-its-the-right-fit-for-this/m-p/558699#M155942</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-05-14T16:57:59Z</dc:date>
    </item>
    <item>
      <title>Re: Trying to use nodupkey but not sure its the right fit for this situation.  Please help!</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Trying-to-use-nodupkey-but-not-sure-its-the-right-fit-for-this/m-p/558700#M155943</link>
      <description>&lt;P&gt;Unfortunately we don't have a reliable upload timestamp.&amp;nbsp; We hoped that the Upload ID would replace that, as it counts up each time we upload a new batch of contracts.&lt;/P&gt;</description>
      <pubDate>Tue, 14 May 2019 17:03:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Trying-to-use-nodupkey-but-not-sure-its-the-right-fit-for-this/m-p/558700#M155943</guid>
      <dc:creator>sirspeedyx0</dc:creator>
      <dc:date>2019-05-14T17:03:29Z</dc:date>
    </item>
    <item>
      <title>Re: Trying to use nodupkey but not sure its the right fit for this situation.  Please help!</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Trying-to-use-nodupkey-but-not-sure-its-the-right-fit-for-this/m-p/558704#M155947</link>
      <description>&lt;P&gt;Unfortunately this doesn't lend itself well to FIRST/LAST though it can probably be done.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's a SQL approach that works, I kept it simple but you can make it more efficient/less steps if needed.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have; 
infile cards dlm=',' truncover dsd;
input ContractID UpLoadID Row;
cards;
1, 1, 1
1, 1, 2
1, 1, 3
1, 1, 4
2, 1, 1
3, 1, 1
3, 1, 2
1, 2, 1
1, 2, 2
1, 2, 3
1, 2, 4
1, 2, 5
2, 2, 1
3, 2, 1
1, 3, 1
1, 3, 3
1, 3, 5
;

proc sql;
create table temp as
select contractID, max(uploadID) as latest_upload
from have
group by contractID;
quit;

proc sql;
create table want as
select t2.*
from temp as t1
inner join have as t2
on t1.contractID=t2.contractID and t1.latest_upload = t2.uploadID
order by 1,2,3;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/274118"&gt;@sirspeedyx0&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Unfortunately we don't have a reliable upload timestamp.&amp;nbsp; We hoped that the Upload ID would replace that, as it counts up each time we upload a new batch of contracts.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 14 May 2019 17:22:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Trying-to-use-nodupkey-but-not-sure-its-the-right-fit-for-this/m-p/558704#M155947</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-05-14T17:22:12Z</dc:date>
    </item>
    <item>
      <title>Re: Trying to use nodupkey but not sure its the right fit for this situation.  Please help!</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Trying-to-use-nodupkey-but-not-sure-its-the-right-fit-for-this/m-p/558734#M155962</link>
      <description>&lt;P&gt;I will be attempting to implement tomorrow, but I wanted to say thanks for this.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;speedy&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 14 May 2019 18:57:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Trying-to-use-nodupkey-but-not-sure-its-the-right-fit-for-this/m-p/558734#M155962</guid>
      <dc:creator>sirspeedyx0</dc:creator>
      <dc:date>2019-05-14T18:57:42Z</dc:date>
    </item>
    <item>
      <title>Re: Trying to use nodupkey but not sure its the right fit for this situation.  Please help!</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Trying-to-use-nodupkey-but-not-sure-its-the-right-fit-for-this/m-p/558764#M155970</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/274118"&gt;@sirspeedyx0&lt;/a&gt;&amp;nbsp;(and btw welcome to the SAS Support Communities! :-)),&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Here's a SQL approach that works, I kept it simple but you can make it more efficient/less steps if needed.&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;The shorter version Reeza mentioned could look like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as
select * from have
group by contractID
having uploadID=max(uploadID)
order by 2,1,3; /* to match the example output */
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 14 May 2019 20:29:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Trying-to-use-nodupkey-but-not-sure-its-the-right-fit-for-this/m-p/558764#M155970</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2019-05-14T20:29:32Z</dc:date>
    </item>
  </channel>
</rss>

