Hello team,
I need to find out the difference between two variables: a dateofservice and two years back from that date of service. In fact, I want to use it in a where clause and say where lookbackperiod is less than or equal to two years. Years can be expressed in term of days as well.
How can I do this in proc sql/ data steps?
Thanks,
blue & blue
Look into the YRDIF() function and then incorporate it into your where clause.
https://go.documentation.sas.com/doc/en/vdmmlcdc/1.0/lefunctionsref/p1pmmr2dtec32an1vbsqmm3abil5.htm
More details may help.
The general function to modify a date value a given amount is INTNX but that requires a base date to calculate from
data example; datevalue = '15Mar2022'd; newdate1 = intnx('year',datevalue, -2,'E'); newdate2 = intnx('year',datevalue, -2,'S'); newdate3 = intnx('year',datevalue, -2,'B'); format datevalue new: date9.; run;
The first parameter is an interval, typical are year, month, week, day though others a acceptable, then the date value to calculate from, then the number of intervals, optional parameters return the End, Beginning or Same date of the interval.
This function can be used with WHERE as long as the date value is available, either a literal date or variable.
Hello teammate,
My datevalue is a variable, which its content are different dates as 11/12/2019 or 0/05/2020, etc, I need to pull dates that are 2 years far from 12,31,2021.
Please give me a chance to read the read the link and come back here.
Regards,
Blue&Blue
I still don't understand what you are trying to do.
Please provide example data, AND the desired result.
What is 'far'? Is it two years ahead or two years behind? Here's an example you can work with and tell us what you want to do.
data have;
input id date :mmddyy10.;
format date mmddyy10.;
datalines;
1 11/12/2019
2 05/05/2020
3 12/31/2019
4 12/31/2018
5 12/31/2020
6 03/04/2022
7 04/05/1980
8 12/31/2022
;
run;
I ended up using the YRDIF function to compute the actual days between the two.
data want;
set have;
years_from = yrdif("31DEC2021"d, date, 'actual');
run;
You can then use FLOOR or CEIL functions to get what you want since we don't know.
data want;
set have;
years_from = yrdif("31DEC2021"d, date, 'actual');
years_ceil = ceil(years_from);
years_floor = floor(years_from);
run;
It's really important that we know what you want. Please always provide example data like I have above. Also provide a data set showing exactly what you want. It allows people to answer the questions and increases the likelihood of people answering your questions.
Hello,
Bringing Example made it more clear. Thanks for it.
blue&blue
Hello,
Can you please explain about ceiling and floor?
thanks,
blue&blue
I suggest you research them yourself first, then if there's something you don't understand or are having trouble applying, ask in here.
Tom,
thanks for this. This helped me in doing my project.
blue&blue
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.