## Converting a numeric, non-integer year value to a SAS date

Solved
Occasional Contributor
Posts: 8

# Converting a numeric, non-integer year value to a SAS date

[ Edited ]

I am working with a small dataset (see attached) that has time coded into fractional years. For example: 1990.1456, 1991.3568, 1991.5689...

Is there an INFORMAT that would allow me to read this variable in as a SAS date?

-John

Accepted Solutions
Solution
‎08-07-2017 10:58 AM
Super User
Posts: 10,283

## Re: Converting a numeric, non-integer year value to a SAS date

Try this:

``````data want;
input dateval;
dateval = round((dateval - 1960) * 365.2425,1);
format dateval yymmddd10.;
cards;
1990.1456
1991.3568
1991.5689
run;
``````
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code

All Replies
Solution
‎08-07-2017 10:58 AM
Super User
Posts: 10,283

## Re: Converting a numeric, non-integer year value to a SAS date

Try this:

``````data want;
input dateval;
dateval = round((dateval - 1960) * 365.2425,1);
format dateval yymmddd10.;
cards;
1990.1456
1991.3568
1991.5689
run;
``````
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Occasional Contributor
Posts: 8

## Re: Converting a numeric, non-integer year value to a SAS date

Thank you for the reply. So that I am understanding correctly, I am basically calculating the non-integer difference in years between my date and the SAS reference date, then converting it to integer days?

This is straight forward. I appreciate it.

Posts: 1,346

## Re: Converting a numeric, non-integer year value to a SAS date

There is no such informat.

However,  you could calculate the date value (or is it really a datetime value?), by

1. Extract the year portion, i.e. the integer
2. Take the decimal portion and determine the corresponding datetime and multply by 24*60*60*(365 or 366) to get number of seconds into the  year if you want date-time values.

``````data want;
set have;
year=floor(time);

/* Get number of seconds in the year, accomodating leap years */
secondsinyear=24*60*60*intck('day',mdy(1,1,year),mdy(1,1,year+1));

datetimevalue=dhms(mdy(1,1,year),0,0,0) +
secondsinyear*mod(time,1);

datevalue=datepart(datetimevalue);
format datetimevalue datetime20. datevalue date9.;
run;``````

Occasional Contributor
Posts: 8

## Re: Converting a numeric, non-integer year value to a SAS date

Thanks for the reply. I suppose you are correct that it could be considered a datetime given its level of accuracy. I'll be sure to give this a try as well.

☑ This topic is solved.

Discussion stats
• 4 replies
• 166 views
• 3 likes
• 3 in conversation