<?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: Appending datasets but must keep unique records by imb_code in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Appending-datasets-but-must-keep-unique-records-by-imb-code/m-p/261233#M50788</link>
    <description>&lt;P&gt;It is quite customary to preprocess the data you want to append by filtering out the rows with a key that's already in the base dataset. A simple SQL query should do (untested):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
  create table newrows as
  select * form MyRuleQuery where ID
     not in (select distinct IMB_CODE from FinalData);
quit;

proc append base=FinalData data = newrows;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Alternatively you van ceate a unique index on the target table.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC DATASETS LIBRARY=yourlib; 
MODIFY Finaldata; 
INDEX CREATE IMB_CODE / UNIQUE NOMISS ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This will reject rows that have a key that is already present. Personally I dislike the (ab)use of a unique index (or actually the associated integrity constraint) as an ETL tool, but is very effective. Apply the unique index just the same as a safety net.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Best of luck,&lt;/P&gt;
&lt;P&gt;- Jan.&lt;/P&gt;
&lt;P&gt;Hope this helps,&lt;/P&gt;
&lt;P&gt;- Jan.&lt;/P&gt;</description>
    <pubDate>Mon, 04 Apr 2016 21:07:11 GMT</pubDate>
    <dc:creator>jklaverstijn</dc:creator>
    <dc:date>2016-04-04T21:07:11Z</dc:date>
    <item>
      <title>Appending datasets but must keep unique records by imb_code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Appending-datasets-but-must-keep-unique-records-by-imb-code/m-p/261209#M50780</link>
      <description>&lt;P&gt;Hi. I have the following macro that iterates multiple times identifying various records. Records are unique by IMB_CODE. With each iteration the identified data is appended into my FinalData dataset.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My problem is that the query can identify the same record (with the same IMB_CODE) in later iterations. When this happens I must have a way to only keep the first instance of the record in my FinalData dataset..&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any suggestions?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%macro BuildQueryData(analysis_desc= , rule= , rule_order= );&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc sql;&lt;BR /&gt;connect to oracle as db (user="me" password="mepw" path="medb");&lt;BR /&gt;create table MyRuleQuery as&lt;BR /&gt;select * from connection to db&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;( select '&amp;amp;analysis_desc' as RULE_NM, b.actual_dlvry_date as AD_DT, b.imb_code, &amp;amp;rule_order as rule_order&lt;BR /&gt;from ivprl.bi_spm_piece_iv_recon a, ivprl.bi_spm_piece_bids_recon b&lt;BR /&gt;where trunc(a.upload_create_dt) = trunc(sysdate)&lt;BR /&gt;and trunc(b.upload_create_dt) = trunc(sysdate)&lt;BR /&gt;and a.imb_code = b.imb_code&lt;BR /&gt;and a.imb_dlvry_zip_3 in(select imb_dlvry_zip_3 from ivprl.bi_spm_piece_bids_recon)&lt;BR /&gt;and a.ml_cl_code in(select ml_cl_code from ivprl.bi_spm_piece_bids_recon)&lt;BR /&gt;and a.ml_cat_code in(select ml_cat_code from ivprl.bi_spm_piece_bids_recon)&lt;BR /&gt;and a.imb_code not in(select imb_code from MyRuleQueryData where rule_order &amp;lt; 999.1)&lt;BR /&gt;and &amp;amp;rule);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;disconnect from db;&lt;BR /&gt;quit;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc append base=FinalData data = MyRuleQuery;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;%mend BuildQueryData;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data _null_;&lt;BR /&gt;set QueryRules;&lt;BR /&gt;call execute('%BuildQueryData(analysis_desc='||analysis_desc||' , rule='||rule||' , rule_order='||rule_order||');');&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Mon, 04 Apr 2016 19:54:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Appending-datasets-but-must-keep-unique-records-by-imb-code/m-p/261209#M50780</guid>
      <dc:creator>buechler66</dc:creator>
      <dc:date>2016-04-04T19:54:49Z</dc:date>
    </item>
    <item>
      <title>Re: Appending datasets but must keep unique records by imb_code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Appending-datasets-but-must-keep-unique-records-by-imb-code/m-p/261227#M50785</link>
      <description>&lt;P&gt;proc sort nodupkey&lt;/P&gt;</description>
      <pubDate>Mon, 04 Apr 2016 20:30:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Appending-datasets-but-must-keep-unique-records-by-imb-code/m-p/261227#M50785</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2016-04-04T20:30:21Z</dc:date>
    </item>
    <item>
      <title>Re: Appending datasets but must keep unique records by imb_code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Appending-datasets-but-must-keep-unique-records-by-imb-code/m-p/261233#M50788</link>
      <description>&lt;P&gt;It is quite customary to preprocess the data you want to append by filtering out the rows with a key that's already in the base dataset. A simple SQL query should do (untested):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
  create table newrows as
  select * form MyRuleQuery where ID
     not in (select distinct IMB_CODE from FinalData);
quit;

proc append base=FinalData data = newrows;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Alternatively you van ceate a unique index on the target table.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC DATASETS LIBRARY=yourlib; 
MODIFY Finaldata; 
INDEX CREATE IMB_CODE / UNIQUE NOMISS ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This will reject rows that have a key that is already present. Personally I dislike the (ab)use of a unique index (or actually the associated integrity constraint) as an ETL tool, but is very effective. Apply the unique index just the same as a safety net.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Best of luck,&lt;/P&gt;
&lt;P&gt;- Jan.&lt;/P&gt;
&lt;P&gt;Hope this helps,&lt;/P&gt;
&lt;P&gt;- Jan.&lt;/P&gt;</description>
      <pubDate>Mon, 04 Apr 2016 21:07:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Appending-datasets-but-must-keep-unique-records-by-imb-code/m-p/261233#M50788</guid>
      <dc:creator>jklaverstijn</dc:creator>
      <dc:date>2016-04-04T21:07:11Z</dc:date>
    </item>
    <item>
      <title>Re: Appending datasets but must keep unique records by imb_code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Appending-datasets-but-must-keep-unique-records-by-imb-code/m-p/261234#M50789</link>
      <description>&lt;P&gt;But I need to guarentee the first instance inserted into the FinalData dataset is the one that stays. &amp;nbsp;I'm not sure NoDupKey guarentees the first record in is not eliminated.&lt;/P&gt;</description>
      <pubDate>Mon, 04 Apr 2016 21:07:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Appending-datasets-but-must-keep-unique-records-by-imb-code/m-p/261234#M50789</guid>
      <dc:creator>buechler66</dc:creator>
      <dc:date>2016-04-04T21:07:43Z</dc:date>
    </item>
  </channel>
</rss>

