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

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 5296 views
  • 2 likes
  • 3 in conversation