BookmarkSubscribeRSS Feed
Q1983
Lapis Lazuli | Level 10

data have;

format mydate date9.;

input ln_no $ code $ mydate date9.;

datalines;

 

1123 C 1jun2018

1123 C 11jun2018

1123 A 15jun2018

1123 A 12jun2018

1123 R 9jun2018

1123 R 3jun2018

1123 A 3May2018

1123 C 12jun2017

;

run;

proc sort data= have out= have2; by ln_no descending mydate;run;

 

How can I delete the last record if the code is anything except an A.  So in this case I want to eliminate the 1123 C  12jun2017 because the code does not equal A

 

12 REPLIES 12
hashman
Ammonite | Level 13

@Q1983:

 

data have ;                                                                                                                             
  format mydate date9.;                                                                                                                 
  input ln_no $ code $ mydate date9. ;                                                                                                  
  cards ;                                                                                                                               
1123 C 1jun2018                                                                                                                         
1123 C 11jun2018                                                                                                                        
1123 A 15jun2018                                                                                                                        
1123 A 12jun2018                                                                                                                        
1123 R 9jun2018                                                                                                                         
1123 R 3jun2018                                                                                                                         
1123 A 3May2018                                                                                                                         
1123 C 12jun2017                                                                                                                        
;                                                                                                                                       
run ;                                                                                                                                   
                                                                                                                                        
data want ;                                                                                                                             
  set have end = z ;                                                                                                                    
  if z and code ne "A" then delete ;                                                                                                    
run ;                        

Kind regards

Paul D. 

Q1983
Lapis Lazuli | Level 10
Lets say I have multiple entries by number like this
data have ;
format mydate date9.;
input ln_no $ code $ mydate date9. ;
cards ;
1123 C 1jun2018
1123 C 11jun2018
1123 A 15jun2018
1123 A 12jun2018
1123 R 9jun2018
1123 R 3jun2018
1123 A 3May2018
1123 C 12jun2017
1126 R 9jun2018
1126 R 3jun2018
1127 A 3May2018
1127 C 12jun2017
;
run ;

data want ;
set have end = z ;
if z and code ne "A" then delete ;
run ;
Can the code be modified to accomplish the same delete of the last record based on number if it is not an A
Jagadishkatam
Amethyst | Level 16
Yes just replace the code ne "A" with the numeric variable and its value
Thanks,
Jag
Q1983
Lapis Lazuli | Level 10
Did not understand your answer. In this case I want to eliminate the last entry which works in your code you sent however I want to also eliminate 1126 R 9jun2018
1126 R 3jun2018 because netiher has a code of A. Also eliminate 1123 C 12jun2017 because it was not preceeded by a code of A, FInally I want to eliminate 1123 C 1jun2018 because it was not preceeded by a code of A
Reeza
Super User
use first/last.

data want;
set have;
by ln_no;

if last.ln_no and code ='C' then delete;
run;
hashman
Ammonite | Level 13

@Q1983;

Methinks you've misstated the problem, hence the confusion. As I understand now, what you meant is "Delete the last record in each BY group if its code is not A". If so: 

data have ;                                  
  format mydate date9.;                      
  input ln_no $ code $ mydate date9. ;       
  cards ;                                    
1123 C 1jun2018                              
1123 C 11jun2018                             
1123 A 15jun2018                             
1123 A 12jun2018                             
1123 R 9jun2018                              
1123 R 3jun2018                              
1123 A 3May2018                              
1123 C 12jun2017                             
1126 R 9jun2018                              
1126 R 3jun2018                              
1127 A 3May2018                              
1127 C 12jun2017                             
;                                            
run ;                                        
                                             
data want ;                                  
  set have ;                                 
  by ln_no ;                                 
  if last.ln_no and code ne "A" then delete ;
run ;                                        

See if that serves you right.

 

Kind regards

Paul D.

Jagadishkatam
Amethyst | Level 16

Please try

 

data want;
set have2 end=eof;
if eof and code ne 'A' then delete;
run;
Thanks,
Jag
novinosrin
Tourmaline | Level 20

Hi @Jagadishkatam  looks like the same answer has been provided by Guru PD 🙂

Jagadishkatam
Amethyst | Level 16
Yes I did not check that answer and when i posted it was same, now i changed the answer with nobs option.
Thanks,
Jag
Q1983
Lapis Lazuli | Level 10
I should have used multiple numbers in my example. Can I accomplish the same thing if I have multiple ln_no
data have ;
format mydate date9.;
input ln_no $ code $ mydate date9. ;
cards ;
1123 C 1jun2018
1123 C 11jun2018
1123 A 15jun2018
1123 A 12jun2018
1123 R 9jun2018
1123 R 3jun2018
1123 A 3May2018
1123 C 12jun2017
1126 R 9jun2018
1126 R 3jun2018
1127 A 3May2018
1127 C 12jun2017
;
run ;

data want ;
set have end = z ;
if z and code ne "A" then delete ;
run ;
Jagadishkatam
Amethyst | Level 16

Alternatively with nobs option

 

data want;
set have2 nobs=nobs;
lastobs=nobs;
if _n_=lastobs and code ne 'A' then delete;
run;

 

 

 

 

Thanks,
Jag
ballardw
Super User

@Q1983 wrote:

data have;

format mydate date9.;

input ln_no $ code $ mydate date9.;

datalines;

 

1123 C 1jun2018

1123 C 11jun2018

1123 A 15jun2018

1123 A 12jun2018

1123 R 9jun2018

1123 R 3jun2018

1123 A 3May2018

1123 C 12jun2017

;

run;

proc sort data= have out= have2; by ln_no descending mydate;run;

 

How can I delete the last record if the code is anything except an A.  So in this case I want to eliminate the 1123 C  12jun2017 because the code does not equal A

 


Notice there is no example of the desired/ expected output. So we have to guess what you actually mean and with out changes in the first variable it can't be determined what the actual role of that variable would be in the solution.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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
  • 12 replies
  • 2615 views
  • 0 likes
  • 6 in conversation