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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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