DATA Step, Macro, Functions and more

to convert a date from date9 format to sas number

Accepted Solution Solved
Reply
Contributor
Posts: 31
Accepted Solution

to convert a date from date9 format to sas number

to convert a date  variable - xdate  to SAS date values like 20340 . Xdate has values -12SEP2015 and xdate is of numeric and format date9

 

 

 

 


Accepted Solutions
Solution
‎10-03-2015 11:04 AM
Super User
Super User
Posts: 6,495

Re: to convert a date from date9 format to sas number

If D is an integer number of days then just add it.

newdate = olddate + d ;

If you want to add other intervals (months, weeks) then you should look at the INTNX() function.

newdate = intnx('day',olddate,d);
newdate = intnx('month',olddate,m,'same');

View solution in original post


All Replies
Trusted Advisor
Posts: 1,204

Re: to convert a date from date9 format to sas number

data have;
date_have='12SEP2015'd;
format date_have date9.;
date_want=date_have;
run;

 

proc print data=have;
run;

Contributor
Posts: 31

Re: to convert a date from date9 format to sas number

How can i add d to my date value.

Solution
‎10-03-2015 11:04 AM
Super User
Super User
Posts: 6,495

Re: to convert a date from date9 format to sas number

If D is an integer number of days then just add it.

newdate = olddate + d ;

If you want to add other intervals (months, weeks) then you should look at the INTNX() function.

newdate = intnx('day',olddate,d);
newdate = intnx('month',olddate,m,'same');
Super User
Super User
Posts: 6,495

Re: to convert a date from date9 format to sas number

So if the variable is of type NUM and has a format DATE9 applied and the values when printed look like valid dates, such as '12SEP2015' then it already IS and number. In fact if the value is '12SEP2015'd then the number in the XDATE variable is 20,343.

 

There is nothing to "convert".

Contributor
Posts: 31

Re: to convert a date from date9 format to sas number

Tom,

 

 The vales in Xdata re of 12SEP2015, 01OCT2015.  when i try to assign it to new variable ydate i need to get the number but i am getting empty values.

 

  ydate = datepart(xdate); - didnt work.

 

 

tried others too.

Super User
Super User
Posts: 6,495

Re: to convert a date from date9 format to sas number

[ Edited ]

The DATEPART() function converts a DATETIME value (number of seconds) to a DATE value (number of days). So basically you divided your date by 86,400 (24hr*60min/hr*60sec/min)

ydate = xdate ;

 

☑ This topic is SOLVED.

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

Discussion stats
  • 6 replies
  • 247 views
  • 2 likes
  • 3 in conversation