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-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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