turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- Analytics
- /
- Stat Procs
- /
- Difference between dates

Topic Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

01-21-2015 05:08 AM

Hi

I have imported an excel file containing two dates columns, formated as yymmdd

I am trying to find the difference between these two columns

I used the code below but it is returning some strange results

**data** want;

set have;

format regtime start_date yymmdd10.;

NumDays = intck( 'day', start_date, regtime );

**run**;

Any help is appreciated

Regards

Accepted Solutions

Solution

01-21-2015
02:01 PM

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

01-21-2015 02:01 PM

I think it is the way that you are importing these.

If you just use a simple proc import, it will import these as numeric and not as SAS dates.

SAS dates are also numeric, but they reference the number of days since 1/1/1960.

What you are getting are the numeric values

Obs Start_date regtime

1 120305 140224

2 120319 140224

3 120227 140224

You need to convert these to SAS dates or import them as SAS dates.

To convert them to SAS dates, you can do something like

**data** want;

set have;

sas_start_date = input(compress(put(start_date,best8.)),yymmdd6.);

sas_regtime = input(compress(put(stat_date,best8.)),yymmdd6.);

NumDays = sas_regtime - sas_start_date;

**run**;

All Replies

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

01-21-2015 05:59 AM

post sample from the have dataset

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

01-21-2015 06:01 AM

Your CODE is Fine.

Better to see your example.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

01-21-2015 06:15 AM

I believe the strange result is because of the date format used.

could you please convert the dates to date9. format and then use the intck function.

Thanks,

Jag

Thanks,

Jag

Jag

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

01-21-2015 03:29 PM

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

01-21-2015 06:29 AM

You don't actually need that function. If both of your variables are date variables, then they are stored as the number of days since a certain timepoint. Therefore if you subtract one from the other you get the number of days between:

num_days = start_date - regtime;

However, I also concur with the above posters, post example data as the term Regtime indicates that you are looking at a time variable not a date. Or possible a date/time variable. If so then just do:

num_days = start_date - datepart(regtime);

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

01-21-2015 01:37 PM

Here is a sample of the file

The variables are not formated as date in the Excel file but I thought it wouldn't be a problem as I am applying a date format in the SAS code then

As I mentioned, the date formate here is yymmdd

Thanks in advance

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

01-21-2015 02:28 PM

Yes, as BurntDirt has mentioned those are just numbers, and it is just guessing what they represent, is 120305 = 12Mar05, or 05Mar12 or something else?? Also little tip, don't use proc import as it basically guesses what you want to do. There are many ways to get data in, I would advise that flat text file delimited, e.g. CSV, is best. Then write a datastep import program specifically tailored to the data you know best.

Solution

01-21-2015
02:01 PM

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

01-21-2015 02:01 PM

I think it is the way that you are importing these.

If you just use a simple proc import, it will import these as numeric and not as SAS dates.

SAS dates are also numeric, but they reference the number of days since 1/1/1960.

What you are getting are the numeric values

Obs Start_date regtime

1 120305 140224

2 120319 140224

3 120227 140224

You need to convert these to SAS dates or import them as SAS dates.

To convert them to SAS dates, you can do something like

**data** want;

set have;

sas_start_date = input(compress(put(start_date,best8.)),yymmdd6.);

sas_regtime = input(compress(put(stat_date,best8.)),yymmdd6.);

NumDays = sas_regtime - sas_start_date;

**run**;