<?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: SQL to Proc SQL in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/SQL-to-Proc-SQL/m-p/540754#M149226</link>
    <description>&lt;P&gt;READ YOUR LOG. In its entirety. That is EXTREMELY important. Really. I mean it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When I run this sample code to re-create your situation:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data class;
set sashelp.class;
where name ne 'Alfred';
run;

proc sql;
insert into class
select * from sashelp.class a
where a.name not in (select name from class where name = a.name)
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I get this log:&lt;/P&gt;
&lt;PRE&gt;32         proc sql;
33         insert into class
34         select * from sashelp.class a
35         where a.name not in (select name from class where name = a.name)
36         ;
WARNING: This DELETE/INSERT statement recursively references the target table. A consequence of this is a possible data integrity 
         problem.
ERROR: You cannot reopen WORK.CLASS.DATA for update access with member-level control because WORK.CLASS.DATA is in use by you in 
resource environment SQL (2).
ERROR: PROC SQL could not undo this statement if an ERROR were to happen as it could not obtain exclusive access to the data set. 
       This statement will not execute as the SQL option UNDO_POLICY=REQUIRED is in effect.
NOTE: PROC SQL set option NOEXEC and will continue to check the syntax of statements.
37         quit;
&lt;/PRE&gt;
&lt;P&gt;You simultaneously open the target dataset for update and read (because of your sub-select), and that is not possible without serious integrity repercussions. The WARNING and the first ERROR alert you to that.&lt;/P&gt;
&lt;P&gt;Save your exclusion list to a helper table:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data class;
set sashelp.class;
where name ne 'Alfred';
run;

proc sql;
create table lookup as
select a.name
from class a inner join sashelp.class b
  on a.name = b.name
;
insert into class
select * from sashelp.class a
where a.name not in (select name from lookup)
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;and Bob's your uncle.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="3"&gt;&lt;STRONG&gt;BIG HINT:&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The &lt;EM&gt;last&lt;/EM&gt; ERROR in your log is always the &lt;EM&gt;least important&lt;/EM&gt;, until you have fixed all WARNINGs/ERRORs &lt;EM&gt;before&lt;/EM&gt; it.&lt;/P&gt;
&lt;P&gt;Debug your code from the top down.&lt;/P&gt;</description>
    <pubDate>Wed, 06 Mar 2019 12:44:03 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2019-03-06T12:44:03Z</dc:date>
    <item>
      <title>SQL to Proc SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-to-Proc-SQL/m-p/540746#M149223</link>
      <description>&lt;P&gt;Hi all&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please refer the code given below in Proc SQL format&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;INSERT INTO School_Reg1
SELECT * FROM School_Reg2 a
WHERE a.student_id not in (select student_id from School_Reg1 where student_id =a.student_id )&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;While I tried Error occurred.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ERROR: PROC SQL could not undo this statement if an ERROR were to happen as it could not obtain exclusive access to&lt;BR /&gt;the data set. This statement will not execute as the SQL option UNDO_POLICY=REQUIRED is in effect.&lt;/P&gt;</description>
      <pubDate>Thu, 07 Mar 2019 03:52:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-to-Proc-SQL/m-p/540746#M149223</guid>
      <dc:creator>Sathish_jammy</dc:creator>
      <dc:date>2019-03-07T03:52:08Z</dc:date>
    </item>
    <item>
      <title>Re: SQL to Proc SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-to-Proc-SQL/m-p/540752#M149225</link>
      <description>&lt;P&gt;you are using Reg1 in you where clause and inserting back in reg1, that is the problem. This is very common issue. You cannot read and write to same dataset at same time.&amp;nbsp;Once you have where clause on Reg1, it will have read lock, so you cannot insert the records i.e. that is insert record.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Think of this as you are going to bank and depositing and taking out money at exactly at same time , it is simply not possible, one transaction has to completed after the another one can start. Your query is violating simple ACID principles. Look for consistency properties in &lt;A href="https://en.wikipedia.org/wiki/ACID_(computer_science" target="_blank"&gt;https://en.wikipedia.org/wiki/ACID_(computer_science&lt;/A&gt;).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If possble create some&amp;nbsp; temporary dataset and insert those records in your reg1.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 06 Mar 2019 12:39:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-to-Proc-SQL/m-p/540752#M149225</guid>
      <dc:creator>kiranv_</dc:creator>
      <dc:date>2019-03-06T12:39:03Z</dc:date>
    </item>
    <item>
      <title>Re: SQL to Proc SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-to-Proc-SQL/m-p/540754#M149226</link>
      <description>&lt;P&gt;READ YOUR LOG. In its entirety. That is EXTREMELY important. Really. I mean it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When I run this sample code to re-create your situation:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data class;
set sashelp.class;
where name ne 'Alfred';
run;

proc sql;
insert into class
select * from sashelp.class a
where a.name not in (select name from class where name = a.name)
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I get this log:&lt;/P&gt;
&lt;PRE&gt;32         proc sql;
33         insert into class
34         select * from sashelp.class a
35         where a.name not in (select name from class where name = a.name)
36         ;
WARNING: This DELETE/INSERT statement recursively references the target table. A consequence of this is a possible data integrity 
         problem.
ERROR: You cannot reopen WORK.CLASS.DATA for update access with member-level control because WORK.CLASS.DATA is in use by you in 
resource environment SQL (2).
ERROR: PROC SQL could not undo this statement if an ERROR were to happen as it could not obtain exclusive access to the data set. 
       This statement will not execute as the SQL option UNDO_POLICY=REQUIRED is in effect.
NOTE: PROC SQL set option NOEXEC and will continue to check the syntax of statements.
37         quit;
&lt;/PRE&gt;
&lt;P&gt;You simultaneously open the target dataset for update and read (because of your sub-select), and that is not possible without serious integrity repercussions. The WARNING and the first ERROR alert you to that.&lt;/P&gt;
&lt;P&gt;Save your exclusion list to a helper table:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data class;
set sashelp.class;
where name ne 'Alfred';
run;

proc sql;
create table lookup as
select a.name
from class a inner join sashelp.class b
  on a.name = b.name
;
insert into class
select * from sashelp.class a
where a.name not in (select name from lookup)
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;and Bob's your uncle.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="3"&gt;&lt;STRONG&gt;BIG HINT:&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The &lt;EM&gt;last&lt;/EM&gt; ERROR in your log is always the &lt;EM&gt;least important&lt;/EM&gt;, until you have fixed all WARNINGs/ERRORs &lt;EM&gt;before&lt;/EM&gt; it.&lt;/P&gt;
&lt;P&gt;Debug your code from the top down.&lt;/P&gt;</description>
      <pubDate>Wed, 06 Mar 2019 12:44:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-to-Proc-SQL/m-p/540754#M149226</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-03-06T12:44:03Z</dc:date>
    </item>
    <item>
      <title>Re: SQL to Proc SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-to-Proc-SQL/m-p/540755#M149227</link>
      <description>&lt;P&gt;This question has been asked several times.&amp;nbsp; Please use the search bar in future before posting.&lt;/P&gt;
&lt;P&gt;E.g.&lt;/P&gt;
&lt;P&gt;&lt;A href="https://communities.sas.com/t5/General-SAS-Programming/UNDO-POLICY-NONE-update-warning-SAS-warnings-are-a-Nanny-state/td-p/283649" target="_blank"&gt;https://communities.sas.com/t5/General-SAS-Programming/UNDO-POLICY-NONE-update-warning-SAS-warnings-are-a-Nanny-state/td-p/283649&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;See the undo_policy option on the proc sql statment.&lt;/P&gt;
&lt;P&gt;Alternatively, and more importantly, why not just use Base SAS?&amp;nbsp; Unless your doing something to a database - and in that case do it on the database - there is no need to resort to SQL.&lt;/P&gt;</description>
      <pubDate>Wed, 06 Mar 2019 12:45:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-to-Proc-SQL/m-p/540755#M149227</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2019-03-06T12:45:08Z</dc:date>
    </item>
    <item>
      <title>Re: SQL to Proc SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-to-Proc-SQL/m-p/541010#M149309</link>
      <description>&lt;P&gt;Dear&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Your code works well.&lt;/P&gt;&lt;P&gt;But I have a little issue,&amp;nbsp;&lt;/P&gt;&lt;P&gt;Once I tried your method with replacement by my tables it performed well, but on few tables it cause ERROR.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro aaa(lu,Tab1,Tab2);

proc sql;
create table &amp;amp;lu as
select a.Student_id
from &amp;amp;Tab1 a inner join &amp;amp;Tab2 b
  on a.Student_id = b.Student_id
;
insert into &amp;amp;Tab1 
select * from &amp;amp;Tab2 a
where a.Student_id not in (select Student_id from &amp;amp;lu)
;
quit;
%mend;
%aaa (reg1,S0.School1,S2.School2);
%aaa (reg2,S0.School1,S3.School3);
%aaa (reg3,S0.School1,S4.School4);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;For the First Macro line&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;88 %aaa(&lt;CODE class=" language-sas"&gt;reg1&lt;/CODE&gt;,&lt;CODE class=" language-sas"&gt;S0.School1&lt;/CODE&gt;,&lt;CODE class=" language-sas"&gt;S2.School2&lt;/CODE&gt;);&lt;BR /&gt;NOTE: Table WORK.REG1 created, with 132489 rows and 1 columns.&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;NOTE: 55148 row was inserted into &lt;CODE class=" language-sas"&gt;S0.School1&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;&lt;CODE class=" language-sas"&gt;/*************************************************************/&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;88 %aaa(&lt;CODE class=" language-sas"&gt;reg2&lt;/CODE&gt;,&lt;CODE class=" language-sas"&gt;S0.School1&lt;/CODE&gt;,&lt;CODE class=" language-sas"&gt;S3.School3&lt;/CODE&gt;);&lt;BR /&gt;NOTE: Table WORK.REG2 created, with 456891 rows and 1 columns.&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;NOTE: 8834 row was inserted into &lt;CODE class=" language-sas"&gt;S0.School1&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;/************************************************************************************************/&lt;/P&gt;&lt;P&gt;While run the Reg3&amp;nbsp; Macro Line&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;P&gt;NOTE: Table WORK.REG3 created, with 643391 rows and 1 columns.&lt;/P&gt;&lt;P&gt;ERROR: CLI execute error: [Microsoft][ODBC SQL Server Driver]Invalid cursor state&lt;BR /&gt;ERROR: ROLLBACK issued due to errors for data set S0.School1.DATA.&lt;BR /&gt;NOTE: The SAS System stopped processing this step because of errors.&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What is this EROOR mean to it.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have no idea about an ERROR.. I google it for its description but I can't find it.&lt;/P&gt;&lt;P&gt;Could you please explain, what cause an ERROR.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in advance!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 07 Mar 2019 08:30:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-to-Proc-SQL/m-p/541010#M149309</guid>
      <dc:creator>Sathish_jammy</dc:creator>
      <dc:date>2019-03-07T08:30:36Z</dc:date>
    </item>
    <item>
      <title>Re: SQL to Proc SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-to-Proc-SQL/m-p/541012#M149311</link>
      <description>&lt;P&gt;Welcome to the fun of adding an unnecessary level of complexity in your process (Micro$oft SQL Server).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Try to get help from MS SQL Server specialists&lt;/LI&gt;
&lt;LI&gt;Move all data to SAS and process there&lt;/LI&gt;
&lt;LI&gt;Move all data to SQL Server and process there, either directly in MS SQL or with explicit pass-through&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;I'm no expert at all with non-SAS SQL and foreign database systems, so I can't help you with that.&lt;/P&gt;</description>
      <pubDate>Thu, 07 Mar 2019 08:42:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-to-Proc-SQL/m-p/541012#M149311</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-03-07T08:42:49Z</dc:date>
    </item>
    <item>
      <title>Re: SQL to Proc SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-to-Proc-SQL/m-p/543036#M150110</link>
      <description>&lt;P&gt;Dear&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I tried the another model in the same method but I failed.&lt;/P&gt;&lt;P&gt;Actually the code u suggested to get new names that works fine.&lt;/P&gt;&lt;P&gt;but What if a person have a same name but with different gender.&lt;/P&gt;&lt;P&gt;For example :&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data class2;
input Name$ Sex$ Age Height Weight;
cards;
Joyce M 12 62.3 55.5
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;In class - Joyce F&lt;/P&gt;&lt;P&gt;in class2 - Joyce M&lt;/P&gt;&lt;P&gt;I tried like :&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table lookup as
select a.name,a.sex
from class a inner join class2 b
  on a.name = b.name
;
insert into class 
select * from class2 a
where a.sex not in (select sexfrom &amp;amp;lu)&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Please correct it and let me know where I'm wrong,&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in advance!&lt;/P&gt;</description>
      <pubDate>Thu, 14 Mar 2019 07:03:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-to-Proc-SQL/m-p/543036#M150110</guid>
      <dc:creator>Sathish_jammy</dc:creator>
      <dc:date>2019-03-14T07:03:55Z</dc:date>
    </item>
    <item>
      <title>Re: SQL to Proc SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-to-Proc-SQL/m-p/543040#M150112</link>
      <description>&lt;P&gt;To do this in SQL, you might need to create a new index variable concatenated from name and sex.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But in that case, I'd use data step programming, where such "compound joins" are much easier.&lt;/P&gt;</description>
      <pubDate>Thu, 14 Mar 2019 07:29:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-to-Proc-SQL/m-p/543040#M150112</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-03-14T07:29:12Z</dc:date>
    </item>
  </channel>
</rss>

