BookmarkSubscribeRSS Feed
deleted_user
Not applicable
I am trying to use 2 fields that I am pulling in with the format 200810 (yyyymm) to calculate the months between these values. The data type in the data set for this field is text. Please let me know how I can calculate the number of months between 2 dates in this format.

I am still new to SAS and so any help will be greatly appreciated. Thanks. Message was edited by: Caesar
3 REPLIES 3
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
You will want to convert your partial date text-string to a SAS numeric variable DATE value (days since 1/1/1960). In a DATA step, you can take your partial string and concatenate a "01" to complete your date string in format "yyyymmdd", and use the ANYDTDTE. or another SAS date-related INFORMAT to derive your SAS numeric DATE variable.

Given your post, perform this task for each of your two partial-date text-string data values, and then use the INTCK function to derive a variable that will represent the months between your two dates.

The SAS support website http://support.sas.com/ has SAS product documentation and technical (user community, SUGI/SGF) papers on this topic. I have provided one example below for your reference.

Scott Barry
SBBWorks, Inc.

Using SAS® Dates and Times – A Tutorial
Jonas V. Bilenas, JP Morgan Chase, Wilmington, DE
http://www2.sas.com/proceedings/forum2007/226-2007.pdf

SAS DOC main page:
http://support.sas.com/documentation/onlinedoc/base/index.html


SAS DOC on INFORMATs by category:
http://support.sas.com/documentation/cdl/en/lrdict/59540/HTML/default/a001239776.htm


Sample doc item on SAS dates/times:
http://support.sas.com/publishing/pubcat/chaps/59411.pdf
Cynthia_sas
SAS Super FREQ
Hi:
The YYMMN informat would allow you to use the INPUT statement to convert your character string to a SAS date value. This informat will use 01 for the date value for any given year/month.

Then, you could either divide to get the number of months (after your subtraction) or you could just use the INTCK function to get the number of months.

See the program below for some possible logic. Of course, you could also substring out the year and month and make your own date value if you wanted some other day to be used (like 15 or 20) instead of 01.

cynthia

[pre]
data makedata;
** make some character strings;
** which are really yyyymm values;
infile datalines;
input ordered $ shipped $;
return;
datalines;
200810 200811
200712 200802
200603 200803
;
run;

proc print data=makedata;
title 'Dates as Character Strings';
run;

data testdur;
set makedata;
** with this INFORMAT, SAS uses 01 as the ;
** day value for the date;
dt_ord = input(ordered,yymmn6.);
dt_shp = input(shipped,yymmn6.);

** 365 days / 12 months = 30.4167;
nummon1 = (dt_shp - dt_ord) / 30.4167;

** Or just use the INTCK function;
nummon2 = intck('month',dt_ord, dt_shp);
format dt_ord dt_shp mmddyy10.;
run;

proc print data=testdur;
title 'Dates Converted to SAS Date Values';
title2 'With 2 diff ways of calculating duration';
run;
[/pre]
deleted_user
Not applicable
Thanks Scott and Cynthia that certainly helped...!

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 3 replies
  • 835 views
  • 0 likes
  • 3 in conversation