<?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: python script to assign id based on dataframe fields in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/SQL-script-to-assign-id-based-on-data-fields/m-p/364219#M275029</link>
    <description>&lt;P&gt;You can put that SQL (assuming it works) into a proc sql statement - SAS has ANSI SQL embedded in it:&lt;/P&gt;
&lt;PRE&gt;proc sql;
  create table want as
select a.customer
   , b.id
from customers a
join id_lookup b 
   on ( a.attr1 = b.attr1
   and a.attr2 = b.attr2
   and a.attr3 = b.attr2 );
quit;&lt;/PRE&gt;
&lt;P&gt;Now I think your problem is bigger than that. &amp;nbsp;You say they can have many lookup values, what about overlap, i.e. Jerry and Tom both have E attribute? &amp;nbsp;Generally speaking I would, and this would also help the SQL, normalise both datasets, so something like:&lt;BR /&gt;CUSTOMER &amp;nbsp;ATTR_NO &amp;nbsp;RESULT&lt;/P&gt;
&lt;P&gt;001 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; a&lt;/P&gt;
&lt;P&gt;001 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; r&lt;/P&gt;
&lt;P&gt;...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can then eradicate rows with nulls. &amp;nbsp;Check for duplicates. &amp;nbsp;Then take the first result which happens (i.e. the lowest), then merge the two together.&lt;/P&gt;</description>
    <pubDate>Mon, 05 Jun 2017 08:05:53 GMT</pubDate>
    <dc:creator>RW9</dc:creator>
    <dc:date>2017-06-05T08:05:53Z</dc:date>
    <item>
      <title>SQL script to assign id based on data fields</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-script-to-assign-id-based-on-data-fields/m-p/364211#M275028</link>
      <description>&lt;P&gt;I'm trying to write an iterative script in sas&amp;nbsp;that would see assign an id to a customer based on whether they've met conditions in my lookup table. Normally, I would accomplish this with a sql join, but I need a procedural script that would see whether they've met the conditions that exist in the lookup, then assign them that id. Some attributes are not required or available, so a sql join would not work, since the join would require that all conditions be met. See the example, below:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;customers table&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;customer attr1 attr2 attr3
jerry    a     r     g
tom      q     e     h
cindy    c     f     j&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;id_lookup table&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;id attr1  attr2  attr3
1  a      (null) g
2 (null)  e      h
3  c      f      (null)&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;final output&lt;/P&gt;
&lt;PRE&gt;customer id
jerry    1
tom      2
cindy    3&lt;/PRE&gt;
&lt;P&gt;Note that jerry had a match on attr1 and attr3, so met the conditions, thus was assigned 1. The script moves on to the next customer to assign an id procedurally, starting at 1 and moving on in ascending order.&lt;BR /&gt;&lt;BR /&gt;in sql, I would write&lt;/P&gt;
&lt;PRE&gt;select a.customer
   , b.id
from customers a
join id_lookup b 
   on ( a.attr1 = b.attr1
   and a.attr2 = b.attr2
   and a.attr3 = b.attr2 )&lt;/PRE&gt;
&lt;P&gt;However, some attributes are not available and since a customer might match many ids, may not be assigned the right once, since the customer should be assigned the id with the lowest value that they qualify for.&lt;/P&gt;
&lt;P&gt;Is there a script in sas that can accomplish this?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 07 Jun 2017 13:24:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-script-to-assign-id-based-on-data-fields/m-p/364211#M275028</guid>
      <dc:creator>superjohn</dc:creator>
      <dc:date>2017-06-07T13:24:04Z</dc:date>
    </item>
    <item>
      <title>Re: python script to assign id based on dataframe fields</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-script-to-assign-id-based-on-data-fields/m-p/364219#M275029</link>
      <description>&lt;P&gt;You can put that SQL (assuming it works) into a proc sql statement - SAS has ANSI SQL embedded in it:&lt;/P&gt;
&lt;PRE&gt;proc sql;
  create table want as
select a.customer
   , b.id
from customers a
join id_lookup b 
   on ( a.attr1 = b.attr1
   and a.attr2 = b.attr2
   and a.attr3 = b.attr2 );
quit;&lt;/PRE&gt;
&lt;P&gt;Now I think your problem is bigger than that. &amp;nbsp;You say they can have many lookup values, what about overlap, i.e. Jerry and Tom both have E attribute? &amp;nbsp;Generally speaking I would, and this would also help the SQL, normalise both datasets, so something like:&lt;BR /&gt;CUSTOMER &amp;nbsp;ATTR_NO &amp;nbsp;RESULT&lt;/P&gt;
&lt;P&gt;001 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; a&lt;/P&gt;
&lt;P&gt;001 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; r&lt;/P&gt;
&lt;P&gt;...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can then eradicate rows with nulls. &amp;nbsp;Check for duplicates. &amp;nbsp;Then take the first result which happens (i.e. the lowest), then merge the two together.&lt;/P&gt;</description>
      <pubDate>Mon, 05 Jun 2017 08:05:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-script-to-assign-id-based-on-data-fields/m-p/364219#M275029</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-06-05T08:05:53Z</dc:date>
    </item>
    <item>
      <title>Re: python script to assign id based on dataframe fields</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-script-to-assign-id-based-on-data-fields/m-p/364259#M275030</link>
      <description>&lt;P&gt;Perl Regular Expression.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data customer;
input customer $ attr1 $ attr2 $ attr3 $;
pid=catx(',',attr1,attr2,attr3);
cards;
jerry    a     r     g
tom      q     e     h
cindy    c     f     j
;
run;

data lookup;
input id attr1 $  attr2  $ attr3 $;
pid='/^'||tranwrd(catx(',',attr1,attr2,attr3),'(null)','.*')||'$/';
cards;
1  a      (null) g 
2 (null)  e      h
3  c      f      (null)
;
run;
proc sql;
select b.*,a.customer,a.pid as a_pid
 from customer as a right join lookup as b 
  on prxmatch(b.pid,strip(a.pid));
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 05 Jun 2017 12:45:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-script-to-assign-id-based-on-data-fields/m-p/364259#M275030</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2017-06-05T12:45:57Z</dc:date>
    </item>
    <item>
      <title>Re: python script to assign id based on dataframe fields</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-script-to-assign-id-based-on-data-fields/m-p/364667#M275031</link>
      <description>&lt;P&gt;awesome. thanks! This totally worked!&lt;/P&gt;</description>
      <pubDate>Tue, 06 Jun 2017 17:53:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-script-to-assign-id-based-on-data-fields/m-p/364667#M275031</guid>
      <dc:creator>superjohn</dc:creator>
      <dc:date>2017-06-06T17:53:32Z</dc:date>
    </item>
  </channel>
</rss>

