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: 13,084

Date time functions

Posted in reply to Danglytics

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: 13,084

Date time functions

Posted in reply to Danglytics

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 ]
Posted in reply to Danglytics

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*/

 

List of SAS Exam and its online practice exams

SAS Certified Base Programmer for SAS 9

SAS Certified Advanced Programmer for SAS 9

SAS Certified Clinical Trials Programmer Using SAS 9

SAS Certified Clinical Trials Programmer Using SAS 9 - Accelerated Version

SAS Certified Statistical Business Analyst Using SAS 9

SAS Certified BI Content Developer for SAS 9

SAS Certified Data Integration Developer for SAS 9

SAS Certified Platform Administrator for SAS 9

SAS Big Data Preparation, Statistics, and Visual Exploration Using SAS 9

SAS Big Data Programming and Loading Using SAS 9

SAS Advanced Predictive Modeling Using SAS 9

SAS Text Analytics, Time Series, Experimentation and Optimization using SAS 9

A00-255: SAS Predictive Modeling Using SAS Enterprise Miner 14

A00-277: SAS Visual Analytics Exploration and Design

A00-262: SAS Certified Data Quality Steward for SAS 9

A00-272: SAS Certified Visual Modeler Using SAS Visual Statistics 7.4


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

Posted in reply to Danglytics

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
  • 168 views
  • 0 likes
  • 4 in conversation