DATA Step, Macro, Functions and more

Formatting Date

Accepted Solution Solved
Reply
Contributor
Posts: 23
Accepted Solution

Formatting Date

Hello,

 

I have a field which contains both time and date, ex: "2018-10-20 10:30"

Does anyone know how I can select only date with condition like the following:

proc sql;

select date_time as date between '2018-01-01' and '2018-01-31"

 

 

Thank you!


Accepted Solutions
Solution
a week ago
Super User
Posts: 23,773

Re: Formatting Date

Whats the type and format of the datetime variable?

If it's a number with a datetime format then you can use:

 

where datepart(date) betweeen '01Jan2018'd and '31Jan2018'd;

@parmis wrote:

Hello,

 

I have a field which contains both time and date, ex: "2018-10-20 10:30"

Does anyone know how I can select only date with condition like the following:

proc sql;

select date_time as date between '2018-01-01' and '2018-01-31"

 

 

Thank you!


 

View solution in original post


All Replies
Regular Contributor
Posts: 164

Re: Formatting Date

datepart: http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000245883.htm

 

i don't know why you'd need to use the condition, extract the date, keep what you want, discard what you don't

--------------
blog: papersandprograms.com
Solution
a week ago
Super User
Posts: 23,773

Re: Formatting Date

Whats the type and format of the datetime variable?

If it's a number with a datetime format then you can use:

 

where datepart(date) betweeen '01Jan2018'd and '31Jan2018'd;

@parmis wrote:

Hello,

 

I have a field which contains both time and date, ex: "2018-10-20 10:30"

Does anyone know how I can select only date with condition like the following:

proc sql;

select date_time as date between '2018-01-01' and '2018-01-31"

 

 

Thank you!


 

Contributor
Posts: 23

Re: Formatting Date

Thank you very much

Super User
Super User
Posts: 8,125

Re: Formatting Date

[ Edited ]

To compare with datetime values you need to use datetime literals.

select date_time 
  from have 
  where date between "01JAN2018:00:00"dt and "31DEC2018:23:59"dt 
;

Or convert it to a date and use date literals.

select date_time 
  from have 
  where datepart(date) between "01JAN2018"d and "31DEC2018"d 
;

Note that the literals must use a format that is recognized by the DATE (or DATETIME) informat. 

☑ This topic is solved.

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

Discussion stats
  • 4 replies
  • 59 views
  • 0 likes
  • 4 in conversation