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);

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!
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.

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