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-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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