DATA Step, Macro, Functions and more

Date Conversions

Reply
N/A
Posts: 0

Date Conversions

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
Super Contributor
Super Contributor
Posts: 3,174

Re: Date Conversions

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
SAS Super FREQ
Posts: 8,739

Re: Date Conversions

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]
N/A
Posts: 0

Re: Date Conversions

Thanks Scott and Cynthia that certainly helped...!
Ask a Question
Discussion stats
  • 3 replies
  • 145 views
  • 0 likes
  • 3 in conversation