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;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 1173 views
  • 0 likes
  • 3 in conversation