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

Hi, In the attached sample data set I have added the input & output data required. I tried to use nodupkey for unique records but did not get the desired output.

 

proc sort data = sasuser.sample nodupkey out=sasuser.sample2;
by number;
where region contains 'A';
run;

 

Only first 3 rows of required output were displayed instead of all 6 rows. 

 

Thanks in Advance. 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Shmuel
Garnet | Level 18

Do it with sql:

 

proc sql;
   create table want as select * 
   from sasuser.sample where number in
       (select distinct number 
from
sasuser.sample
where region = "A") ; quit;

 

 

View solution in original post

4 REPLIES 4
Shmuel
Garnet | Level 18

Do it with sql:

 

proc sql;
   create table want as select * 
   from sasuser.sample where number in
       (select distinct number 
from
sasuser.sample
where region = "A") ; quit;

 

 

dhir
Obsidian | Level 7

Thank you, Shmuel. works perfectly.

mkeintz
PROC Star

@Shmuel's suggestion is the most compact.  But if this is really about how to use proc sort for your purpose, then you should (1) sort ALL the records number, not just the 'A' records, and (2) read back the sorted file, with the 'A' record preceding non-'A' records for each number, and (3) keep the desired records:

 

[editted on Wed 7/5 - there is no IF function.  I should have used IFN function, corrected below]

 

proc sort data=have out=need;

  by number;

run;

 

data want;

  set have (where=(region='A'))   have (where=(region^='A'));

  by number;

  if first.number then keeprec=ifn(region='A',1,0);

  retain keeprec;

  if keeprec;

run;

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
dhir
Obsidian | Level 7

Hi mkeintz,

 

Thank you for the reply.

 

First part of code worked, need created but second part returned the attached error. 

 

I have changed the file names in your code: 

 

proc sort data=sasuser.sample out=sasuser.need;
by number;
run;

data want;
set sasuser.need (where=(region='A')) sasuser.need (where=(region^='A'));
by number;
if first.number then keeprec=if(region='A',1,0);
retain keeprec;
if keeprec;
run;

 

Help me correct any mistakes here. 

 

Thank you.

 

 

 


1.png

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 4 replies
  • 5906 views
  • 2 likes
  • 3 in conversation