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. 

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!

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