BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
gamotte
Rhodochrosite | Level 12

Hi,

 

I would like to update the following sample dataset

data test;
infile datalines dlm=',';
input id cat $ status $;
datalines;
1,A,KO,
2,A,KO,
3,A,KO,
2,B,NR
;
run;

The rule is to set the status of a category A (cat="A") individual to "OK" if he also belongs to the category B.

 

With the above example, the second row would become :

2,A,OK

 

I thought of using a proc sql as follows :

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;

But this generates an error :

ERROR: You cannot reopen WORK.TEST.DATA for update access with member-level control because
WORK.TEST.DATA is in use by you in resource environment SQL.

 

I could, in a first step, create a second dataset with the ids of all B individuals but i try, when i can, to

avoid intermediary steps that distract the reader from the main program goal.

 

Is the lock on the dataset a limitation of SAS or is it possible to refer the same dataset in the SET

and the WHERE clauses of an UPDATE ?

 

Thanks !

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

No, you can't sub-qeury a dataset which is open for update.  It could create a cycle which never ends.  What is the purpose of the code.  Simpest way to make your code work, duplciate the dataset:

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;
update TEST A set STATUS="OK" where A.CAT="A" and A.ID in (select distinct ID from TEST2 where CAT="B"); quit;

 There are however other ways of doing it, datastep, sorting etc.

View solution in original post

4 REPLIES 4
RW9
Diamond | Level 26 RW9
Diamond | Level 26

No, you can't sub-qeury a dataset which is open for update.  It could create a cycle which never ends.  What is the purpose of the code.  Simpest way to make your code work, duplciate the dataset:

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;
update TEST A set STATUS="OK" where A.CAT="A" and A.ID in (select distinct ID from TEST2 where CAT="B"); quit;

 There are however other ways of doing it, datastep, sorting etc.

gamotte
Rhodochrosite | Level 12

Hi,

 

It seems to me that this kind of things is possible under other flavors of SQL (the subquey being evaluated first,

there is no cycle), that is why i asked the question.

I will thus duplicate the dataset as you advised me.

 

Thanks.

Ksharp
Super User
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;
gamotte
Rhodochrosite | Level 12

Thank you for the tip. I will avoid to use such options as, like you say, it can be risky.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 2607 views
  • 0 likes
  • 3 in conversation