Converting to SAS Dates to Use Datdiff or Intck Function

Accepted Solution Solved
Reply
New Contributor
Posts: 3
Accepted Solution

Converting to SAS Dates to Use Datdiff or Intck Function

Hi,

I'm new to SAS and I think I converted the two different date fields in this data type:  01DEC2013:00:00:00 by using the following code:

data ME_&dtwo._&dtwo2.;

length SAS_Date $10;

set ME_&dtwo._&dtwo2.;

SAS_Date=put(datepart(ME_date),mmddyy10.);

RUN;

data ME_&dtwo._&dtwo2.;

length SAS_Date2 $10;

set ME_&dtwo._&dtwo2.;

SAS_Date2=put(datepart(ME2_date),mmddyy10.);

RUN;

But I can't seem to use the datdiff or Intck function to calculate the number of months between the two date fields.  What am I missing?  Are the converted date fields incorrect?

I've tried using a datastep and proc sql to calculate the number of months in  between, but nothing is working.  The Proc SQL code that I'm using is below:

create table ME_&dtwo._&dtwo2. as

(SELECT

NumofMonth=intck('month','SAS_Date', 'SAS_Date2'),

ban.SAS_Date,

ban.SAS_Date2

From Another_table);

RUN;

Can someone please help me?  I've been searching the web for hours looking for a solution.

thanks!


Accepted Solutions
Solution
‎06-03-2014 01:21 PM
Respected Advisor
Posts: 4,648

Re: Converting to SAS Dates to Use Datdiff or Intck Function

Most of SAS programming can be done without macros. Macro programs and variables add a layer of complexity over your code and should only be added AFTER your code is tested.

As said, SAS dates and datetimes are numeric, not character, variables. Date and Datetime formats are only needed for display purposes, but for calculations, such as determining the number of months between two dates, you must provide raw numeric values :

data ME_&dtwo._&dtwo2.;

set ME_&dtwo._&dtwo2.;

NumofMonths = intck("MONTH", datepart(ME_date), datepart(ME2_date) );

RUN;


PG

PG

View solution in original post


All Replies
Super User
Posts: 5,256

Re: Converting to SAS Dates to Use Datdiff or Intck Function

SAS dates are numerical, so skip the put() and change your length statements.

Data never sleeps
New Contributor
Posts: 3

Re: Converting to SAS Dates to Use Datdiff or Intck Function

Thank you LinusH!

I'm unsure of what you mean about skipping the put() and change the length.  Could you provide an example?

thanks!

Solution
‎06-03-2014 01:21 PM
Respected Advisor
Posts: 4,648

Re: Converting to SAS Dates to Use Datdiff or Intck Function

Most of SAS programming can be done without macros. Macro programs and variables add a layer of complexity over your code and should only be added AFTER your code is tested.

As said, SAS dates and datetimes are numeric, not character, variables. Date and Datetime formats are only needed for display purposes, but for calculations, such as determining the number of months between two dates, you must provide raw numeric values :

data ME_&dtwo._&dtwo2.;

set ME_&dtwo._&dtwo2.;

NumofMonths = intck("MONTH", datepart(ME_date), datepart(ME2_date) );

RUN;


PG

PG
New Contributor
Posts: 3

Re: Converting to SAS Dates to Use Datdiff or Intck Function

THANK YOU so much for the clarification!  Now, I understand what you mean that the dates are already in a SAS date format!  This worked great and I appreciate your feedback!

☑ This topic is SOLVED.

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

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