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
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.
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.
Please try
data want;
set have2 end=eof;
if eof and code ne 'A' then delete;
run;
Hi @Jagadishkatam looks like the same answer has been provided by Guru PD 🙂
Alternatively with nobs option
data want;
set have2 nobs=nobs;
lastobs=nobs;
if _n_=lastobs and code ne 'A' then delete;
run;
@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.
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!
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.
Ready to level-up your skills? Choose your own adventure.