BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
parmis
Fluorite | Level 6

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!

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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

4 REPLIES 4
pau13rown
Lapis Lazuli | Level 10

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

Reeza
Super User

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!


 

parmis
Fluorite | Level 6

Thank you very much

Tom
Super User Tom
Super User

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. 

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 4 replies
  • 1559 views
  • 0 likes
  • 4 in conversation