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

Hi,

 

I have two columns in a table, Program name and Part numbers. I need to find out the part numbers which are unique to a program Name. The output can contain the same part number within a program but not in any other program.

 

Here is the dataset.

 

data have;
input pname $ part $;
datalines;
program1 001
program2 002
program3 001
program3 004
program4 005 program4 005 program5 005 ; run;

have.png

 

Want this

want.png

Subhro Kar
www.9to5sas.com
1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

Essentially this means you want the parts that have a single program name. 

Or at least that's what it seems but your output doesn't match your description. Part 1 is in Program 1 and Program 3? I thought you may want the opposite but the P005 is in Program 4/5 and you have it included for Program4. 

 

The output can contain the same part number within a program but not in any other program.

 

In general, this is how you do something like this though:

proc sql;
create table want as
select * from have where part in (
select part
from have
group by part
having count(distinct pname)=1);


quit;

@subhroster wrote:

Hi,

 

I have two columns in a table, Program name and Part numbers. I need to find out the part numbers which are unique to a program Name. The output can contain the same part number within a program but not in any other program.

 

Here is the dataset.

 

data have;
input pname $ part $;
datalines;
program1 001
program2 002
program3 001
program3 004
program4 005 program4 005 program5 005 ; run;

have.png

 

Want this

want.png


 

View solution in original post

3 REPLIES 3
Reeza
Super User

Essentially this means you want the parts that have a single program name. 

Or at least that's what it seems but your output doesn't match your description. Part 1 is in Program 1 and Program 3? I thought you may want the opposite but the P005 is in Program 4/5 and you have it included for Program4. 

 

The output can contain the same part number within a program but not in any other program.

 

In general, this is how you do something like this though:

proc sql;
create table want as
select * from have where part in (
select part
from have
group by part
having count(distinct pname)=1);


quit;

@subhroster wrote:

Hi,

 

I have two columns in a table, Program name and Part numbers. I need to find out the part numbers which are unique to a program Name. The output can contain the same part number within a program but not in any other program.

 

Here is the dataset.

 

data have;
input pname $ part $;
datalines;
program1 001
program2 002
program3 001
program3 004
program4 005 program4 005 program5 005 ; run;

have.png

 

Want this

want.png


 

subhroster
Fluorite | Level 6

Hi Reeza

Subhro Kar
www.9to5sas.com
Ksharp
Super User
data have;
input pname $ part $;
datalines;
program1 001
program2 002
program3 001
program3 004
program4 005
program4 005
program5 005
;
run;



data want;
 if _n_=1 then do;
  if 0 then set have;
  declare hash h1();
  h1.definekey('part');
  h1.definedone();
  declare hash h2();
  h2.definekey('part','pname');
  h2.definedone();
 end;
set have;
if h1.check() ne 0 then do;h1.add();h2.ref();output;end;
 else if h2.check()=0 then output;
run;

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 397 views
  • 2 likes
  • 3 in conversation