DATA Step, Macro, Functions and more

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

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 8
Accepted Solution

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?

 

Thank you for any help you can offer. 

 

-John

Attachment

Accepted Solutions
Solution
2 weeks ago
Super User
Posts: 6,977

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

View solution in original post


All Replies
Solution
2 weeks ago
Super User
Posts: 6,977

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

Valued Guide
Posts: 797

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.

Need further help from the community? Please ask a new question.

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