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
@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.
@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.
You could try using the lag function:
datebefore = lag(date);
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.
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.
Ready to level-up your skills? Choose your own adventure.