BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
sophia_SAS
Obsidian | Level 7

I have a range of dates.  I would like to count how many months are within each set of dates along with creating a new variable for each month. For example;

data have;

input start_date end_date;

01/01/2010 12/31/2010

05/01/2010 12/31/2010

05/01/2010 05/14/2010

06/13/2010 08/28/2010;

run;

I would like to create a new variable for each month that contains the counts for each number of months between the start and end dates. For example;

01/01/2010 12/31/2010 --> month1=1 month2=1, month3=1 . . . .month12=1;

05/01/2010 12/31/2010 --> month5=1 month6=1, month7=1 . . . .month12=1;

05/01/2010 05/14/2010 --> month5=1;

06/13/2010 08/28/2010 --> month6=1 month7=1 month8=1;

Based on this sample data, in the end I should have the below counts for each month.

Month1=1

Month2=1

Month3=1

Month4=1

Month5=3

Month6=3

Month7=3

Month8=3

Month9=2

Month10=2

Month11=2

Month12=2

Thank you!

Sophia

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

Are all of your data pairs always within the same calendar year? Are the dates SAS date values or strings?

For your example this seems to work.

 

data have;

informat start_date end_date mmddyy10.;

format start_date end_date mmddyy10.;

input start_date end_date;

datalines;

01/01/2010 12/31/2010

05/01/2010 12/31/2010

05/01/2010 05/14/2010

06/13/2010 08/28/2010

;

run;

data want;

set have;

array months month1-month12;

do i= month(start_date) to month(end_date);

months=1;

end;

drop i;

run;

View solution in original post

2 REPLIES 2
ballardw
Super User

Are all of your data pairs always within the same calendar year? Are the dates SAS date values or strings?

For your example this seems to work.

 

data have;

informat start_date end_date mmddyy10.;

format start_date end_date mmddyy10.;

input start_date end_date;

datalines;

01/01/2010 12/31/2010

05/01/2010 12/31/2010

05/01/2010 05/14/2010

06/13/2010 08/28/2010

;

run;

data want;

set have;

array months month1-month12;

do i= month(start_date) to month(end_date);

months=1;

end;

drop i;

run;

sophia_SAS
Obsidian | Level 7

Thanks!  That worked perfectly.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 972 views
  • 0 likes
  • 2 in conversation