DATA Step, Macro, Functions and more

Finding number of days each month between dates

Reply
Occasional Contributor
Posts: 11

Finding number of days each month between dates

Hi I am looking for a code that does the feature by JP in this thread. The issue is that if the year of the start and end date is different it does not work.

Example, Dec 2, 2014 and ends January 3, 2015.

Thank you

Valued Guide
Posts: 860

Re: Finding number of days each month between dates

Based on your question I'm not sure what you are asking for.  Is this what you want?

data have;

format date1 date2 mmddyy10.;

date1 = '02DEC2014'd;

date2 = '03JAN2015'd;

run;

data want;

set have;

date_diff = intck('day',date1,date2);

run;

Super User
Super User
Posts: 7,988

Re: Finding number of days each month between dates

Posted in reply to Steelers_In_DC

As they are date variables, you don't actually need to use a function:

data want;

set have;

date_diff = date2 - date1;

run;

Occasional Contributor
Posts: 11

Re: Finding number of days each month between dates

Sorry the link didn't show up:

https://communities.sas.com/thread/51733

I am trying to find another code that does similar to this. The issue with this it does not work for cross year boundaries i.e December 2014 and January 2015

Super User
Super User
Posts: 7,988

Re: Finding number of days each month between dates

Post some test data.  I personally would expand your data for each date  then sum it up, note to transpose you would need to know year and month e.g.

data have;

  effective_date="01JAN2014"d;

  end_date="14FEB2015"d;

  output;

run;

data inter;

  set have;

  do day_date=effective_date to end_date;

    mnth=month(day_date);

    yr=year(day_date);

    output;

  end;

run;

proc sql;

  create table WANT as

  select  MNTH,

          YR,

          COUNT(1) as CNT

  from    INTER

  group by YR,

            MNTH;

quit;

Occasional Contributor
Posts: 11

Re: Finding number of days each month between dates

Here are some sample data:

Visit Number     Checkin Date  CheckoutDate

234212/25/20141/6/2015
123412/27/20141/1/2015

Should be this:

Visit Number     Checkin Date  CheckoutDate   Dec 2014    Jan 2015

234212/25/20141/6/201576
123412/27/20141/1/201551
Super User
Super User
Posts: 7,988

Re: Finding number of days each month between dates

Yes, my program should work on that.  I wouldn't go with having dates as columns though.  Its a bad idea in the longer term, I mean if you had a time period (not necessarily on one row, but over all data) of several years, you can end up with lots of columns with few data in.  Better to keep it normalised as my output will do, or genericise the columns, e.g. COL1-COLx, so you can use arrays and fill up only the ones you need.

Occasional Contributor
Posts: 11

Re: Finding number of days each month between dates

Hey RW9, I just tried and yes I can use it!

Thank you

Ask a Question
Discussion stats
  • 7 replies
  • 216 views
  • 4 likes
  • 3 in conversation