DATA Step, Macro, Functions and more

Extract date only from datetime format

Reply
Highlighted
Regular Contributor
Posts: 179

Extract date only from datetime format

data mydate (keep=ln_no mydate);

set date1;

run;

output is

ln_no    mydate

011     09JUN2015:00:00:00.000

 

I tried using this
newdt = datepart(mydate);

format newdt mmddyy10.

For some reason the newdt turns into a numeric and looses its date datatype.

I want to extract the 09JUN2015 since the datepart function on this particular field does not convert to a date.

 

Super User
Posts: 23,771

Re: Extract date only from datetime format

 

For some reason the newdt turns into a numeric and looses its date datatype.

 

SAS Dates, times and datetimes are stored as numeric variables. 

 

Is your starting variable a numeric variable with a datetime format?

If it's character, DATEPART() will not work. In that case, you can use SCAN instead with the : as the delimiter and then use INPUT to convert it to a SAS date. 

Trusted Advisor
Posts: 1,837

Re: Extract date only from datetime format

It seems that the variable MYDATE is a char type - that is a character string, 

then you can read it either by defining its length $9 with informat date9. (neglect the time part)

or if you have it as one char type variable of length $24 then:

     datex = scan(mydate,1,':');

     date = input(datex,date9.);

     format date date9.;

 

Ask a Question
Discussion stats
  • 2 replies
  • 226 views
  • 0 likes
  • 3 in conversation