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

@Kurt_Bremser @hhinohar 

Hi I have a query please advise. 

Data I have:

IDTxFlag
1NAUnknown
1yesY
1yesY
2NAUnknown
3NAUnknown
3yesY
3yesY

 

Data I Want:

IDTxFlag
1yesY
1yesY
2NAUnknown
3yesY
3yesY

 

I want to delete those records which has Tx as 'NA' and Flag as 'Unknown', however if there is only one record for any ID, I dont want to delete that record even if the condition satisfies. 

 

Ex: For ID 2 there is only one record but it satisfies my condition to delete but I dont want to delete ID 2 as there is only one record. 

 

Please advise. 

 

Thank you

 

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

Try this

 

data have;
input ID Tx $ Flag $;
datalines;
1 NA  Unknown
1 yes Y      
1 yes Y      
2 NA  Unknown
3 NA  Unknown
3 yes Y      
3 yes Y      
;

data want;
   set have;
   by id;
   if Tx = 'NA' & Flag = 'Unknown' & sum(first.id, last.id) ne 2 then delete;
run;

 

Result:

 

ID Tx  Flag 
1  yes Y 
1  yes Y 
2  NA  Unknown 
3  yes Y 
3  yes Y 

View solution in original post

5 REPLIES 5
PeterClemmensen
Tourmaline | Level 20

Try this

 

data have;
input ID Tx $ Flag $;
datalines;
1 NA  Unknown
1 yes Y      
1 yes Y      
2 NA  Unknown
3 NA  Unknown
3 yes Y      
3 yes Y      
;

data want;
   set have;
   by id;
   if Tx = 'NA' & Flag = 'Unknown' & sum(first.id, last.id) ne 2 then delete;
run;

 

Result:

 

ID Tx  Flag 
1  yes Y 
1  yes Y 
2  NA  Unknown 
3  yes Y 
3  yes Y 
hhinohar
Quartz | Level 8

Oh gee.I did a slight test of double if. I hope somebody corrects it.

data have;
infile datalines dlm="09"x;
input ID Tx $ Flag $;
datalines;
1	NA	Unknown
1	yes	Y
1	yes	Y
2	NA	Unknown
3	NA	Unknown
3	yes	Y
3	yes	Y
;
run;

data want;
	set have;
	by id;
	*Double if statement;
	if tx="NA" and upcase(FLAG)="UNKNOWN" then
		if not first.id or not last.id then delete;
run;
bharath86
Obsidian | Level 7
Hey,

Thank you for the response. All three solutions are working actually. I just haveto accept the first one as he deserves it.
Kurt_Bremser
Super User
data want;
set have;
by id;
if (first.id and last.id /* only one obs */) or tx ne "NA" or flag ne "Unknown";
run;

If it is possible that an id has only multiple "NA"/"Unknown" observations, a double DO loop must be used to determine that beforehand.

bharath86
Obsidian | Level 7
Hey,
Thank you for the response. You been amazingly helping me here.

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
  • 5 replies
  • 712 views
  • 1 like
  • 4 in conversation