<?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: PROC SQL - Update table using another row from the same table in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-Update-table-using-another-row-from-the-same-table/m-p/312379#M67752</link>
    <description>&lt;P&gt;No, you can't sub-qeury a dataset which is open for update. &amp;nbsp;It could create a cycle which never ends. &amp;nbsp;What is the purpose of the code. &amp;nbsp;Simpest way to make your code work, duplciate the dataset:&lt;/P&gt;
&lt;PRE&gt;data test test2;
  infile datalines dlm=',';
  input id cat $ status $;
datalines;
1,A,KO,
2,A,KO,
3,A,KO,
2,B,NR
;
run;

proc sql;&lt;BR /&gt;  update TEST A
  set STATUS="OK"
  where A.CAT="A" 
  and A.ID in (select distinct ID from TEST2 where CAT="B");
quit;
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;There are however other ways of doing it, datastep, sorting etc.&lt;/P&gt;</description>
    <pubDate>Thu, 17 Nov 2016 16:35:47 GMT</pubDate>
    <dc:creator>RW9</dc:creator>
    <dc:date>2016-11-17T16:35:47Z</dc:date>
    <item>
      <title>PROC SQL - Update table using another row from the same table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-Update-table-using-another-row-from-the-same-table/m-p/312370#M67748</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would like to update the following sample dataset&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
infile datalines dlm=',';
input id cat $ status $;
datalines;
1,A,KO,
2,A,KO,
3,A,KO,
2,B,NR
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The rule is to set the status of a category A (cat="A") individual to "OK" if he also belongs to the category B.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;With the above example, the second row would become :&lt;/P&gt;
&lt;P&gt;2,A,OK&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I thought of using a proc sql as follows :&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
	UPDATE test a
	SET status="OK"
	WHERE a.cat="A" and a.id in (
		SELECT b.id FROM test b
		WHERE b.cat="B"
	);
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But this generates an error :&lt;/P&gt;
&lt;P&gt;ERROR: You cannot reopen WORK.TEST.DATA for update access with member-level control because&lt;BR /&gt;WORK.TEST.DATA is in use by you in resource environment SQL.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I could, in a first step, create a second dataset with the ids of all B individuals but i try, when i can, to&lt;/P&gt;
&lt;P&gt;avoid intermediary steps that distract the reader from the main program goal.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is the lock on the dataset a limitation of SAS or is it possible to refer the same dataset in the SET&lt;/P&gt;
&lt;P&gt;and the WHERE clauses of an UPDATE ?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks !&lt;/P&gt;</description>
      <pubDate>Thu, 17 Nov 2016 16:12:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-Update-table-using-another-row-from-the-same-table/m-p/312370#M67748</guid>
      <dc:creator>gamotte</dc:creator>
      <dc:date>2016-11-17T16:12:55Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL - Update table using another row from the same table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-Update-table-using-another-row-from-the-same-table/m-p/312379#M67752</link>
      <description>&lt;P&gt;No, you can't sub-qeury a dataset which is open for update. &amp;nbsp;It could create a cycle which never ends. &amp;nbsp;What is the purpose of the code. &amp;nbsp;Simpest way to make your code work, duplciate the dataset:&lt;/P&gt;
&lt;PRE&gt;data test test2;
  infile datalines dlm=',';
  input id cat $ status $;
datalines;
1,A,KO,
2,A,KO,
3,A,KO,
2,B,NR
;
run;

proc sql;&lt;BR /&gt;  update TEST A
  set STATUS="OK"
  where A.CAT="A" 
  and A.ID in (select distinct ID from TEST2 where CAT="B");
quit;
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;There are however other ways of doing it, datastep, sorting etc.&lt;/P&gt;</description>
      <pubDate>Thu, 17 Nov 2016 16:35:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-Update-table-using-another-row-from-the-same-table/m-p/312379#M67752</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2016-11-17T16:35:47Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL - Update table using another row from the same table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-Update-table-using-another-row-from-the-same-table/m-p/312384#M67755</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It seems to me that this kind of things is possible under other flavors of SQL (the subquey being evaluated first,&lt;/P&gt;
&lt;P&gt;there is no cycle), that is why i asked the question.&lt;/P&gt;
&lt;P&gt;I will thus duplicate the dataset as you advised me.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks.&lt;/P&gt;</description>
      <pubDate>Thu, 17 Nov 2016 16:45:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-Update-table-using-another-row-from-the-same-table/m-p/312384#M67755</guid>
      <dc:creator>gamotte</dc:creator>
      <dc:date>2016-11-17T16:45:07Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL - Update table using another row from the same table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-Update-table-using-another-row-from-the-same-table/m-p/312550#M67817</link>
      <description>&lt;PRE&gt;
you can change option to do it. But it is risky.



data test;
  infile datalines dlm=',';
  input id cat $ status $;
datalines;
1,A,KO,
2,A,KO,
3,A,KO,
2,B,NR
;
run;
proc sql undo_policy=none nowarn;
	UPDATE test a
	SET status="OK"
	WHERE a.cat="A" and
    exists(select * from test where id=a.id and cat='B')
;
quit;
&lt;/PRE&gt;</description>
      <pubDate>Fri, 18 Nov 2016 08:53:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-Update-table-using-another-row-from-the-same-table/m-p/312550#M67817</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-11-18T08:53:37Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL - Update table using another row from the same table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-Update-table-using-another-row-from-the-same-table/m-p/312558#M67820</link>
      <description>&lt;P&gt;Thank you for the tip. I will avoid to use such options as, like you say, it can be risky.&lt;/P&gt;</description>
      <pubDate>Fri, 18 Nov 2016 09:25:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-Update-table-using-another-row-from-the-same-table/m-p/312558#M67820</guid>
      <dc:creator>gamotte</dc:creator>
      <dc:date>2016-11-18T09:25:01Z</dc:date>
    </item>
  </channel>
</rss>

