BookmarkSubscribeRSS Feed
sasuser123123
Quartz | Level 8
Hello !

I've a variable 'date' which is numeric variable it contains date 29Dec08:10:00:00 so I need to convert into days. so how to convert this date to days . I'm wrote
Newdate=input(put (date,datetime18.), datetime18.),
But I got wrong numeric dates.. could you please solve this query..

Thank you.!
Regards..
8 REPLIES 8
Kurt_Bremser
Super User

You do not have a date, you have a datetime. Datetimes are counts of seconds, dates are counts of days. To extract the date from a datetime, use the datepart() function.

Your double function call keeps the value as it is, only cutting off any fractions of seconds, which could be achieved easier with round().

Shmuel
Garnet | Level 18

I don't know what you mean by converting a date into days,

but may be you mean:

      date = datepart(datetime_variable);

sasuser123123
Quartz | Level 8
Thank you for suggestion, actually I'm trying to calculate age from two variables. While calculating age should we consider time and seconds or not
Shmuel
Garnet | Level 18

having to variables you can calculate age by subtracting years:

 

      age = intck('year', datetime2, datetime1);

or

     age = intck('year', date2, date1);

ballardw
Super User

@Kurt_Bremser wrote:

You have to use "dtyear" as the interval.


Or DTDAY, DTMONTH , DTWEEK  (see a pattern here?).

JackHamilton
Lapis Lazuli | Level 10

The code 

 

    age = intck('year', date2, date1);

 

will not always produce the result you might expect, because what it counts is the number of occurences of January 1 between the two dates:

 

 69         data _null_;
 70             years = intck('year', '31dec2019'd, '01jan2020'd);
 71             put years=;
 72         run;
 
 years=1

Use of the CONTINUOUS option might produce the result you want:

 

data _null_;
    years = intck('year', '31dec2019'd, '01jan2020'd, 'continuous');
    put years=;
run;

The traditional method of calculating age in SAS, attributed to Billy Kreuter, is 

 

age =  floor(( intck( 'month', dob, death) - ( day(death) < day(dob)))/ 12); 

<https://www.lexjansen.com/pharmasug/2011/CC/PharmaSUG-2011-CC20.pdf>

 

There's a lazy method that just takes the difference in days and divides by 365.25.  Please don't use that method.

 

ballardw
Super User

@sasuser123123 wrote:
Thank you for suggestion, actually I'm trying to calculate age from two variables. While calculating age should we consider time and seconds or not

That would be YOUR requirement to know.

 

Hours seldom become a consideration unless dealing with newborn babies though.

 

 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 8 replies
  • 3965 views
  • 7 likes
  • 5 in conversation