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

Hi Team,

I need help for below scenario.

All dated will be in ascending order and if any date is smaller than its previous date then that is odd and need to be filter out.

I applied lag function but it is getting applied on entire variable not on group if 1 and 2 separately.

 

data test1;
input id date$12.;
datalines;
1 01JAN2010
1 04JAN2010
1 08JAN2010
1 07JAN2010
1 11JAN2010
2 05JAN2010
2 06JAN2010
2 04JAN2010
2 07JAN2010
2 10JAN2010
;
RUN;

 

Expected output:

1 07JAN2010

2 04JAN2010

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

Before doing anything else, convert the strings to SAS dates by using the INPUT function with the DATE9. informat.

Then it's easy:

data want;
set test1;
by id;
if date lt lag(date) and not first.id;
run;

View solution in original post

1 REPLY 1
Kurt_Bremser
Super User

Before doing anything else, convert the strings to SAS dates by using the INPUT function with the DATE9. informat.

Then it's easy:

data want;
set test1;
by id;
if date lt lag(date) and not first.id;
run;

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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
  • 1 reply
  • 410 views
  • 0 likes
  • 2 in conversation