How To Calc Month Difference between two Numeric Columns (YYYYMM)

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 9
Accepted Solution

How To Calc Month Difference between two Numeric Columns (YYYYMM)

Hi people,

I have searched in the google and forum but did not find the solution,

here is my problem:

I have two column in my table  date1 and date2 , columns are "numeric" and the date formats represents YYYYMM

date1         date2           datediff

---------        -----------  ----------------

201303      201310      ??

201211      201305       ??

I want to find how many month difference between these two date

according to example it must be   7 and the second row must be 6  month

but i could not find how to calculate in sas  enterprise guide 5.1  ?


Accepted Solutions
Solution
‎12-23-2013 11:18 AM
Trusted Advisor
Posts: 1,131

Re: How To Calc Month Difference between two Numeric Columns (YYYYMM)

Hi,

Please try the below code

data have;

    input date1 date2;

    date1_=input(strip(put(date1,6.))||'01',yymmdd10.);

    date2_=input(strip(put(date2,6.))||'01',yymmdd10.);

    datediff=intck('month',date1_,date2_);

cards;

201303      201310     

201211      201305

;

Thanks,

Jag

Thanks,
Jag

View solution in original post


All Replies
Solution
‎12-23-2013 11:18 AM
Trusted Advisor
Posts: 1,131

Re: How To Calc Month Difference between two Numeric Columns (YYYYMM)

Hi,

Please try the below code

data have;

    input date1 date2;

    date1_=input(strip(put(date1,6.))||'01',yymmdd10.);

    date2_=input(strip(put(date2,6.))||'01',yymmdd10.);

    datediff=intck('month',date1_,date2_);

cards;

201303      201310     

201211      201305

;

Thanks,

Jag

Thanks,
Jag
Occasional Contributor
Posts: 9

Re: How To Calc Month Difference between two Numeric Columns (YYYYMM)

Thank you very much,

all my best wishes with you in your life Smiley Happy

Contributor
Posts: 57

Re: How To Calc Month Difference between two Numeric Columns (YYYYMM)

This should do it.

data work.cd;

input date1 date2;

datalines;

201303 201310

201211 201305

;

run;

data work.temp;

set work.cd;

datediff=intck('MONTH', input(put(date1, 6.), yymmn6.), input(put(date2, 6.), yymmn6.));

run;

☑ This topic is solved.

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

Discussion stats
  • 3 replies
  • 6532 views
  • 3 likes
  • 3 in conversation