BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
abraham1
Obsidian | Level 7

Hi Everyone!

For my study, I want to retrieve records from all phases when one hour prior to  first visit time i.e. visit_F ({HH:MM}-1) is greater than last visit time (HH:MM)

In database, all dates are stored in character format.

 

data visit;
input phase visit_F $20. visit_E $20.;
cards;
1 2012-04-24T19:22:03 2012-04-24T20:23:03
2 2012-04-22T07:22:03 2012-04-22T07:22:01
3 2012-04-24T19:22:03 2012-04-24T16:23:03
;
run;
1 ACCEPTED SOLUTION

Accepted Solutions
s_lassen
Meteorite | Level 14

As @Kurt_Bremser remarked, you should change the data to numeric datetimes, if possible. If not, you will have to do the conversion on the fly, e.g.:

data want;                                                                                                                              
  set visit;                                                                                                                            
  dt_first=input(visit_f,e8601dt.);                                                                                                     
  dt_last=input(visit_e,e8601dt.);                                                                                                      
  format dt_: datetime22.;                                                                                                              
  if dt_first-3600>dt_last;                                                                                                             
run;

The 3600 is (of course) the number of seconds in an hour, SAS datetime values are recorded as the number of seconds since January first, 1960.

View solution in original post

2 REPLIES 2
s_lassen
Meteorite | Level 14

As @Kurt_Bremser remarked, you should change the data to numeric datetimes, if possible. If not, you will have to do the conversion on the fly, e.g.:

data want;                                                                                                                              
  set visit;                                                                                                                            
  dt_first=input(visit_f,e8601dt.);                                                                                                     
  dt_last=input(visit_e,e8601dt.);                                                                                                      
  format dt_: datetime22.;                                                                                                              
  if dt_first-3600>dt_last;                                                                                                             
run;

The 3600 is (of course) the number of seconds in an hour, SAS datetime values are recorded as the number of seconds since January first, 1960.

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and save with the early bird rate—just $795!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 782 views
  • 1 like
  • 3 in conversation