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.

 

 

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 8 replies
  • 4260 views
  • 7 likes
  • 5 in conversation