DATA Step, Macro, Functions and more

Date time functions

Accepted Solution Solved
Reply
Contributor
Posts: 66
Accepted Solution

Date time functions

Hi,

If I have a set date, and want to create date variables that are, 1 month, 2 month 6 month before that set date, is there a function that will be able to get me that?

for example:

set_date = 12DEC11

i want to create a new var with date 1 month before the set_date:

set_date_last_1mth = 12NOV11 ;

2 months:

set_date_last_2mth = 12OCT11 ;

is there a simple way to do this?

currently I use set_date_last_1mth = set_date - 30 ; -60 -120 etc. which isnt entirely accurate or the best way to do this.

Thanks for your help.


Accepted Solutions
Solution
‎12-19-2011 12:45 PM
Super User
Posts: 10,500

Date time functions

Have you explored the INTNX function yet? There are lots of examples in the online help that look like what your are attempting.

View solution in original post


All Replies
Solution
‎12-19-2011 12:45 PM
Super User
Posts: 10,500

Date time functions

Have you explored the INTNX function yet? There are lots of examples in the online help that look like what your are attempting.

New Contributor
Posts: 2

Re: Date time functions

[ Edited ]

Hi, 

 

These are the functions used to process date and time values.

 

For Example: 

 

data date_functions;
INPUT @1 date1 date9. @11 date2 date9.;
format date1 date9. date2 date9.;

/* Get the interval between the dates in years*/
Years_ = INTCK(‘YEAR’,date1,date2);

/* Get the interval between the dates in months*/
months_ = INTCK(‘MONTH’,date1,date2);

/* Get the week day from the date*/
weekday_ = WEEKDAY(date1);

/* Get Today’s date in SAS date format */
today_ = TODAY();

/* Get current time in SAS time format */
time_ = time();
DATALINES;
21OCT2000 16AUG1998
01MAR2009 11JUL2012
;
proc print data = date_functions noobs;
run;

 

Output:

date_time_funcs_result.jpg

Senior User
Posts: 1

Re: Date time functions

Hello,

 

Please use beow code to resolve your query in most efficient way:

 

data out1;

set_date = '12dec2011'd;

set_date_last_1mth = intnx('month',set_date,-1,'s');

set_date_last_2mth = intnx('month',set_date,-2,'s');

format set_date set_date_last_1mth set_date_last_2mth ddmmyy10.;

run;

 

I hope it helps. Thanks.

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 146 views
  • 0 likes
  • 4 in conversation