<?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: Delete data from one table, linking to another table using SQL in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Delete-data-from-one-table-linking-to-another-table-using-SQL/m-p/30402#M7214</link>
    <description>I can think of a few approaches:&lt;BR /&gt;
&lt;BR /&gt;
&lt;B&gt;Approach One&lt;/B&gt;:   This assumes that cur_street_address1 and cur_city_name are strings with the same definition between tables.&lt;BR /&gt;
Proc sql;&lt;BR /&gt;
  delete from mylib.ResidentA &lt;BR /&gt;
     where cur_street_address1 || cur_city_name in (select cur_street_address1 || cur_city_name from mylib.AddressDel);&lt;BR /&gt;
quit;&lt;BR /&gt;
&lt;BR /&gt;
&lt;B&gt;Approach Two&lt;/B&gt;:  This assumes there is a unique ID (say U_ID) that can be mapped between the two tables.&lt;BR /&gt;
&lt;BR /&gt;
Proc sql;&lt;BR /&gt;
  delete from mylib.ResidentA &lt;BR /&gt;
     where U_ID  in (select U_ID from mylib.ResidentA  a&lt;BR /&gt;
                              inner join  mylib.AddressDel d &lt;BR /&gt;
                              where a.cur_street_address1 = d.cur_street_address1 and&lt;BR /&gt;
                                       a.cur_city_name = d.cur_city_name);&lt;BR /&gt;
quit;&lt;BR /&gt;
&lt;BR /&gt;
&lt;B&gt;Approach Three:&lt;/B&gt;  Why not just create a new table....</description>
    <pubDate>Tue, 09 Nov 2010 00:04:42 GMT</pubDate>
    <dc:creator>Grim</dc:creator>
    <dc:date>2010-11-09T00:04:42Z</dc:date>
    <item>
      <title>Delete data from one table, linking to another table using SQL</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Delete-data-from-one-table-linking-to-another-table-using-SQL/m-p/30401#M7213</link>
      <description>I have two tables:&lt;BR /&gt;
ResidentA and AddressDel&lt;BR /&gt;
&lt;BR /&gt;
both tables contain the fields cur_street_address1 and cur_city_name&lt;BR /&gt;
&lt;BR /&gt;
I am trying to write a SQL code to delete the records in ResidentA that are in AddressDel by cur_street_address1 and cur_city_name&lt;BR /&gt;
&lt;BR /&gt;
Is there anyway to do this?&lt;BR /&gt;
&lt;BR /&gt;
Here is what i have, and i know it is wrong.  I use to do SQL on an AS400 mainframe and this is one i just cant figure out.&lt;BR /&gt;
&lt;BR /&gt;
proc sql;&lt;BR /&gt;
	delete p.*  from mylib.ResidentAIDS as p, mylib.FederalPrisonWV as q&lt;BR /&gt;
	where p.cur_street_address1 = q.cur_street_address1 and&lt;BR /&gt;
                          p.cur_city_name = q.cur_city_name;&lt;BR /&gt;
run;</description>
      <pubDate>Mon, 08 Nov 2010 20:54:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Delete-data-from-one-table-linking-to-another-table-using-SQL/m-p/30401#M7213</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2010-11-08T20:54:30Z</dc:date>
    </item>
    <item>
      <title>Re: Delete data from one table, linking to another table using SQL</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Delete-data-from-one-table-linking-to-another-table-using-SQL/m-p/30402#M7214</link>
      <description>I can think of a few approaches:&lt;BR /&gt;
&lt;BR /&gt;
&lt;B&gt;Approach One&lt;/B&gt;:   This assumes that cur_street_address1 and cur_city_name are strings with the same definition between tables.&lt;BR /&gt;
Proc sql;&lt;BR /&gt;
  delete from mylib.ResidentA &lt;BR /&gt;
     where cur_street_address1 || cur_city_name in (select cur_street_address1 || cur_city_name from mylib.AddressDel);&lt;BR /&gt;
quit;&lt;BR /&gt;
&lt;BR /&gt;
&lt;B&gt;Approach Two&lt;/B&gt;:  This assumes there is a unique ID (say U_ID) that can be mapped between the two tables.&lt;BR /&gt;
&lt;BR /&gt;
Proc sql;&lt;BR /&gt;
  delete from mylib.ResidentA &lt;BR /&gt;
     where U_ID  in (select U_ID from mylib.ResidentA  a&lt;BR /&gt;
                              inner join  mylib.AddressDel d &lt;BR /&gt;
                              where a.cur_street_address1 = d.cur_street_address1 and&lt;BR /&gt;
                                       a.cur_city_name = d.cur_city_name);&lt;BR /&gt;
quit;&lt;BR /&gt;
&lt;BR /&gt;
&lt;B&gt;Approach Three:&lt;/B&gt;  Why not just create a new table....</description>
      <pubDate>Tue, 09 Nov 2010 00:04:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Delete-data-from-one-table-linking-to-another-table-using-SQL/m-p/30402#M7214</guid>
      <dc:creator>Grim</dc:creator>
      <dc:date>2010-11-09T00:04:42Z</dc:date>
    </item>
    <item>
      <title>Re: Delete data from one table, linking to another table using SQL</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Delete-data-from-one-table-linking-to-another-table-using-SQL/m-p/30403#M7215</link>
      <description>Hi.Recommend you to post up some dummy data and what data do you need.&lt;BR /&gt;
Not origin data structure ,it is hard to code.&lt;BR /&gt;
As what you write .Consider use proc sql 's 'except' operation .&lt;BR /&gt;
&lt;BR /&gt;
[pre]&lt;BR /&gt;
data temp;&lt;BR /&gt;
 set sashelp.class;&lt;BR /&gt;
 where sex eq 'F';&lt;BR /&gt;
run;&lt;BR /&gt;
proc sql feedback;&lt;BR /&gt;
 select * from sashelp.class&lt;BR /&gt;
  except&lt;BR /&gt;
 select * from temp;&lt;BR /&gt;
quit;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
&lt;BR /&gt;
Ksharp</description>
      <pubDate>Tue, 09 Nov 2010 07:43:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Delete-data-from-one-table-linking-to-another-table-using-SQL/m-p/30403#M7215</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2010-11-09T07:43:59Z</dc:date>
    </item>
    <item>
      <title>Re: Delete data from one table, linking to another table using SQL</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Delete-data-from-one-table-linking-to-another-table-using-SQL/m-p/30404#M7216</link>
      <description>Hi &lt;BR /&gt;
&lt;BR /&gt;
The way this is often solved in SQL is to concatenate the columns, i.e:&lt;BR /&gt;
&lt;BR /&gt;
data ResidentA;&lt;BR /&gt;
  othervar=1;&lt;BR /&gt;
  cur_city_name       = 'City 1';&lt;BR /&gt;
  cur_street_address1 = 'Street 1';&lt;BR /&gt;
  output;&lt;BR /&gt;
  cur_city_name       = 'City 1';&lt;BR /&gt;
  cur_street_address1 = 'Street 2';&lt;BR /&gt;
  output;&lt;BR /&gt;
  cur_city_name       = 'City 2';&lt;BR /&gt;
  cur_street_address1 = 'Street 1';&lt;BR /&gt;
  output;&lt;BR /&gt;
  cur_city_name       = 'City 2';&lt;BR /&gt;
  cur_street_address1 = 'Street 2';&lt;BR /&gt;
  output;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
data AddressDel;&lt;BR /&gt;
  cur_city_name       = 'City 1';&lt;BR /&gt;
  cur_street_address1 = 'Street 2';&lt;BR /&gt;
  output;&lt;BR /&gt;
  cur_city_name       = 'City 1';&lt;BR /&gt;
  cur_street_address1 = 'Street 3';&lt;BR /&gt;
  output;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
proc sql;&lt;BR /&gt;
  delete from ResidentA&lt;BR /&gt;
    where cats(cur_city_name,'|',cur_street_address1) in&lt;BR /&gt;
          ( select cats(cur_city_name,'|',cur_street_address1) from AddressDel )&lt;BR /&gt;
  ;&lt;BR /&gt;
quit;&lt;BR /&gt;
&lt;BR /&gt;
proc print data=ResidentA;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
I used a '|' as separator when concatenating the variables. Use whatever character or string you like. Just make sure that when combining vars that the combined string is only possible for the row(s) where the variables come from. Best use a character which you're sure that it's not used in any of the variables.&lt;BR /&gt;
&lt;BR /&gt;
By the way:&lt;BR /&gt;
If the data is in SAS tables then a more efficient way would be to load the AddressDel into a hash with the 2 fields as key and then just to delete all records in ResidentA where the check() method doesn't find a match. You could even implement this with a modify statement which means only one pass through ResidentA with change in place.&lt;BR /&gt;
&lt;BR /&gt;
HTH&lt;BR /&gt;
Patrick

Message was edited by: Patrick</description>
      <pubDate>Tue, 09 Nov 2010 09:41:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Delete-data-from-one-table-linking-to-another-table-using-SQL/m-p/30404#M7216</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2010-11-09T09:41:10Z</dc:date>
    </item>
    <item>
      <title>Re: Delete data from one table, linking to another table using SQL</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Delete-data-from-one-table-linking-to-another-table-using-SQL/m-p/30405#M7217</link>
      <description>Thanks for the ideas everyone.  I used Grim's approach 1 and it worked.  I did not know you could use a select within the statement.&lt;BR /&gt;
&lt;BR /&gt;
Thanks!!!</description>
      <pubDate>Tue, 09 Nov 2010 14:30:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Delete-data-from-one-table-linking-to-another-table-using-SQL/m-p/30405#M7217</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2010-11-09T14:30:43Z</dc:date>
    </item>
  </channel>
</rss>

