BookmarkSubscribeRSS Feed

I'm trying to only pick those primary keys with a code equal to A2391 AND A2392 ONLY - for each primarykey. 

 

PrimaryKey    Proc Code

a156273.      A2391

a156273.      A2392

c456373.      A2391

c456373.      A2392

c456373.      A2393

b784734.      A2391

b784734.     A2392

 

Should look like this

a156273.      A2391

a156273.      A2392

b784734.      A2391

b784734.      A2392

c456373.      A2391

c456373.      A2392

 

So for each primary key, I want to bring in only those who have those codes.

5 REPLIES 5
Reeza
Super User

This is one of those cases where it's easier if you reverse your logic

1. Find IDs that have something other than those two values. 

2. Exclude those IDs

 

proc sql;
create table want as
select * 
from have 
where primaryKey not in 
         ( select distinct PrimaryKey 
            from have 
            where proc_code not in ('A2391', 'A2392')
          );
quit;

@anonymous_user wrote:

I'm trying to only pick those primary keys with a code equal to A2391 AND A2392 ONLY - for each primarykey. 

 

PrimaryKey    Proc Code

a156273.      A2391

a156273.      A2392

c456373.      A2391

c456373.      A2392

c456373.      A2393

b784734.      A2391

b784734.     A2392

 

Should look like this

a156273.      A2391

a156273.      A2392

b784734.      A2391

b784734.      A2392

c456373.      A2391

c456373.      A2392

 

So for each primary key, I want to bring in only those who have those codes.


 

Thank you for your response.

I'm getting a syntax error - ERROR: 22-322: after where it says:

where PrimaryKey not in

Reeza
Super User
Post your exact code and log. I tested your code against the data provided and it works fine with no errors so there's either a typo or your data doesn't match what's shown. I'm assuming you changed the variable and table names to match your data.

log:

 

Proc Sql;

create table s.mergefinal as

select *

from s.merge

where PrimaryKey not in

ERROR 22-322: Syntax error, expecting one of the following: GROUP, HAVING, ORDER, 

 

ERROR 76-322: Syntax error

 

(select distinct PrimaryKey from merge

where proc_code not in ('2391','2392')

);

quit;

NOTE: The SAS System stopped processing this step because of errors.

Reeza
Super User
That isn't your full code and log.

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
  • 5 replies
  • 803 views
  • 0 likes
  • 2 in conversation