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

Hi all,

 

I have a question regarding to how can I compare Date column with same value but in previous line. I will post an example, because maybe it will be better to understand.

 

I have a dataset with the following data:

 

Nr            Date                      DocumentDate

1              17APR2019          3APR2019       

1              31MAY2019          6APR2019

1              26APR2019          18APR2019

2              22APR2019          5APR2019

2              1MAY2019            6APR2019

2              14APR2019          8APR2019

 

DocumentDate is the date where the line enter to the dataset.

 

What I want to accomplish is another dataset with the following information. This dataset is a tracking of how 'Date' column has been changed.

 

Order   DateBefore       NewDate           DocumentDate

1                 .                 17APR2019       3APR2019

1          17APR2019      31MAY2019      6APR2019

1          31MAY2019     26APR2019       18APR2019

2               .                  22APR2019        5APR2019

2          22APR2019     1MAY2019         6APR2019

2          1MAY2019       14APR2019       8APR2019

 

Please suggest a code sample or a documentation where I can find a data step to accomplish this task.

Thanks in advance.

 

Regards

 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

@mm84 wrote:

Hi all,

 

I have a question regarding to how can I compare Date column with same value but in previous line. I will post an example, because maybe it will be better to understand.

 

I have a dataset with the following data:

 

Nr            Date                      DocumentDate

1              17APR2019          3APR2019       

1              31MAY2019          6APR2019

1              26APR2019          18APR2019

2              22APR2019          5APR2019

2              1MAY2019            6APR2019

2              14APR2019          8APR2019

 

DocumentDate is the date where the line enter to the dataset.

 

What I want to accomplish is another dataset with the following information. This dataset is a tracking of how 'Date' column has been changed.

 

Order   DateBefore       NewDate           DocumentDate

1                 .                 17APR2019       3APR2019

1          17APR2019      31MAY2019      6APR2019

1          31MAY2019     26APR2019       18APR2019

2               .                  22APR2019        5APR2019

2          22APR2019     1MAY2019         6APR2019

2          1MAY2019       14APR2019       8APR2019

 

Please suggest a code sample or a documentation where I can find a data step to accomplish this task.

Thanks in advance.

 

Regards

 


Please post example data in the form of a data step:

data have;
   input Nr  Date :date9.  DocumentDate :date9.;
   format date documentdate date9.;
datalines;
1    17APR2019  3APR2019       
1    31MAY2019  6APR2019
1    26APR2019  18APR2019
2    22APR2019  5APR2019
2    1MAY2019  6APR2019
2    14APR2019  8APR2019
run;

data want;
   set have;
   by nr;
   datebefore=lag(date);
   format datebefore date9.;
   if first.nr then datebefore=.;
run;

I am not sure why you have "newdate" in the wanted data set as it is identical to the date variable. You also do not mention the specific comparison you may want.

If your data is not sorted by NR and DATE then you would need to do that before the WANT data step.

View solution in original post

2 REPLIES 2
ballardw
Super User

@mm84 wrote:

Hi all,

 

I have a question regarding to how can I compare Date column with same value but in previous line. I will post an example, because maybe it will be better to understand.

 

I have a dataset with the following data:

 

Nr            Date                      DocumentDate

1              17APR2019          3APR2019       

1              31MAY2019          6APR2019

1              26APR2019          18APR2019

2              22APR2019          5APR2019

2              1MAY2019            6APR2019

2              14APR2019          8APR2019

 

DocumentDate is the date where the line enter to the dataset.

 

What I want to accomplish is another dataset with the following information. This dataset is a tracking of how 'Date' column has been changed.

 

Order   DateBefore       NewDate           DocumentDate

1                 .                 17APR2019       3APR2019

1          17APR2019      31MAY2019      6APR2019

1          31MAY2019     26APR2019       18APR2019

2               .                  22APR2019        5APR2019

2          22APR2019     1MAY2019         6APR2019

2          1MAY2019       14APR2019       8APR2019

 

Please suggest a code sample or a documentation where I can find a data step to accomplish this task.

Thanks in advance.

 

Regards

 


Please post example data in the form of a data step:

data have;
   input Nr  Date :date9.  DocumentDate :date9.;
   format date documentdate date9.;
datalines;
1    17APR2019  3APR2019       
1    31MAY2019  6APR2019
1    26APR2019  18APR2019
2    22APR2019  5APR2019
2    1MAY2019  6APR2019
2    14APR2019  8APR2019
run;

data want;
   set have;
   by nr;
   datebefore=lag(date);
   format datebefore date9.;
   if first.nr then datebefore=.;
run;

I am not sure why you have "newdate" in the wanted data set as it is identical to the date variable. You also do not mention the specific comparison you may want.

If your data is not sorted by NR and DATE then you would need to do that before the WANT data step.

LNEW
Obsidian | Level 7

You could try using the lag function:

datebefore = lag(date);

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 2 replies
  • 1650 views
  • 0 likes
  • 3 in conversation