BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
GN0001
Barite | Level 11

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

 

Blue Blue
1 ACCEPTED SOLUTION
10 REPLIES 10
ballardw
Super User

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.

GN0001
Barite | Level 11

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

Blue Blue
PaigeMiller
Diamond | Level 26

I still don't understand what you are trying to do.

 

Please provide example data, AND the desired result.

--
Paige Miller
maguiremq
SAS Super FREQ

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;

maguiremq_0-1646416593980.png

 

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.

 

GN0001
Barite | Level 11

Hello, 

Bringing Example made it more clear. Thanks for it.

blue&blue

Blue Blue
GN0001
Barite | Level 11

Hello, 

Can you please explain about ceiling and floor?

thanks, 

blue&blue

Blue Blue
ChanceTGardener
SAS Employee

I suggest you research them yourself first, then if there's something you don't understand or are having trouble applying, ask in here. 

 

ChanceTGardener_0-1646588411980.png

 

GN0001
Barite | Level 11

Tom,

thanks for this. This helped me in doing my project. 
blue&blue

Blue Blue

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

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.

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
  • 10 replies
  • 705 views
  • 5 likes
  • 5 in conversation