Hello,
i have a data set with id and service date and i want to know if each service date are 6 month apart or not. i use the lag function but it give me a weird result. Please help me figure out how this works. thank you so much.
data test1;
set test;
by id;
if first.id then date=.;
else date = lag(servicedate);
format date mmddyy10.;
run;
id | Count | servicedate | year |
1 | 0 | 11/8/2013 | 2013 |
1 | 20 | 11/7/2014 | 2014 |
1 | 20 | 2/2/2015 | 2015 |
1 | 20 | 4/23/2015 | 2015 |
1 | 0 | 10/8/2015 | 2015 |
1 | 0 | 1/5/2016 | 2016 |
1 | 20 | 4/6/2016 | 2016 |
1 | 40 | 6/30/2016 | 2016 |
2 | 20 | 8/21/2015 | 2015 |
2 | 20 | 8/21/2015 | 2015 |
data test1;
set test;
by id;
prev_date=lag(servicedate);
if first.id then date=.;
else date = prev_date;
format date mmddyy10.;
run;
Thanks.
i found my question is more complicated than i thought. i actually need to find those dates that has 6 month or greater interval with any of the previous date value. for example, the value of the newDate variable only keep those dates that have 6 month or greater than any of the previous dates in the servicedate. How to create the newDate variable? please help.
ID | servicedate | NewDate |
1 | 11/8/2013 | 11/8/2013 |
1 | 12/7/2013 | |
1 | 2/2/2014 | 2/2/2014 |
1 | 4/23/2014 | |
1 | 9/8/2014 | 9/8/2014 |
1 | 12/5/2014 | |
1 | 4/6/2015 | 4/6/2015 |
2 | 3/17/2016 | 3/17/2016 |
2 | 4/17/2016 | |
2 | 9/18/2016 | 9/18/2016 |
2 | 12/5/2016 | |
2 | 12/9/2016 | |
2 | 4/9/2016 | 4/9/2016 |
2 | 11/21/2016 | 11/21/2016 |
2 | 11/21/2016 |
Sorry, but i can't understand the logic you have described. Why is "4/23/2014" not in the NewDate variable? There are observations with id=1 having a difference of more than six months-
@juliajulia wrote:
Thanks.
i found my question is more complicated than i thought. i actually need to find those dates that has 6 month or greater interval with any of the previous date value. for example, the value of the newDate variable only keep those dates that have 6 month or greater than any of the previous dates in the servicedate. How to create the newDate variable? please help.
ID servicedate NewDate 1 11/8/2013 11/8/2013 1 12/7/2013 1 2/2/2014 2/2/2014 1 4/23/2014 1 9/8/2014 9/8/2014 1 12/5/2014 1 4/6/2015 4/6/2015 2 3/17/2016 3/17/2016 2 4/17/2016 2 9/18/2016 9/18/2016 2 12/5/2016 2 12/9/2016 2 4/9/2016 4/9/2016 2 11/21/2016 11/21/2016 2 11/21/2016
data test1;
set test;
by id;
prev_date=lag(servicedate);
if not first.id and intck('month',prev_date,servicedate,'c')>=6
then newdate=date;
format newdate mmddyy10.;
run;
Use the INTCK function to determine how many months have elapsed between the two dates.
I note, as did @andreas_lds that your word description does not match the output you say you want.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.