BookmarkSubscribeRSS Feed
radhikaa4
Calcite | Level 5

Following table:

namestatus version
ABCDnot recruiting4
ABCDrecruiting4
EFGHnot recruiting4
HIJKrecruiting4
LMNOnot recruiting4
LMNOrecruiting4
QRSTnot recruiting

4

 

Want to only export second entry of the name

 

namestatus version
ABCDrecruiting4
EFGHnot recruiting4
HIJKrecruiting4
LMNOrecruiting4
QRSTnot recruiting4

 

I tried using row number but i am losing version

4 REPLIES 4
novinosrin
Tourmaline | Level 20

data have;
input name $	status & $30. 	version;
cards;
ABCD	not recruiting	 4
ABCD	recruiting	 4
EFGH	not recruiting	 4
HIJK	recruiting	 4
LMNO	not recruiting	 4
LMNO	recruiting	 4
QRST	not recruiting   4
;

proc sql;
create table want(drop=m) as
select *,monotonic() as m 
from have
group by name
having max(m)=m
order by m;
quit;
Reeza
Super User

Use FIRST.

 

This handles the cases of a single record (by name) and two records but will not handle a case where you have three records for name. 

If that's the case you'll need to add in a counter variable and expand the IF condition. 

data want;
set have;
by name;
if not first.name or (first.name and last.name);
run;

 

 

EDIT: It seems like you just want the recruiting records. Any reason to not use that as the filter rule? 

where status='recruiting';
novinosrin
Tourmaline | Level 20

Slight variation to not have assumption of sets on 2

 

data have;
input name $	status & $30. 	version;
cards;
ABCD	not recruiting	 4
ABCD	recruiting	 4
EFGH	not recruiting	 4
HIJK	recruiting	 4
LMNO	not recruiting	 4
LMNO	recruiting	 4
QRST	not recruiting   4
;

proc sql;
create table want(drop=m) as
select *,monotonic() as m
from have
group by name
having min(m)+1=m or count(*)=1
order by m;
quit;
novinosrin
Tourmaline | Level 20

And the obvious, but lately I am becoming bored of obvious ones though

 

data have;
input name $	status & $30. 	version;
cards;
ABCD	not recruiting	 4
ABCD	recruiting	 4
EFGH	not recruiting	 4
HIJK	recruiting	 4
LMNO	not recruiting	 4
LMNO	recruiting	 4
QRST	not recruiting   4
;
data want;
 do _n_=1 by 1 until(last.name);
  set have;
  by name;
  if _n_=2 then output;
 end;
 if _n_=1 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!
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
  • 472 views
  • 0 likes
  • 3 in conversation