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

Sorry, this code doesnt work, but trying to sort the datetime variable

 

data have;
input datetime;
format datetime datetime.;
infile datalines missover;
datalines;
01AUG2022:11:51:35
22JUL2019:11:41:32
22JUL2019:11:50:32
25FEB2015:16:05:39
;
run;

1 ACCEPTED SOLUTION

Accepted Solutions
japelin
Rhodochrosite | Level 12

try this input statement.

 

input datetime:datetime.;

View solution in original post

3 REPLIES 3
ywon111
Quartz | Level 8
The usual proc sort doesn't work and it just sorted it by date, didn't take in consideration of the time.
japelin
Rhodochrosite | Level 12

try this input statement.

 

input datetime:datetime.;
ballardw
Super User

That data step as shown will not generate any output data values to sort. Without a proper informat specified the input statement tells SAS to read the values as basic numeric values. But the : occurs as part of the value so is treated as not valid as a number.

 

586  data have;
587  input datetime;
588  format datetime datetime.;
589  infile datalines missover;
590  datalines;

NOTE: Invalid data for datetime in line 591 1-18.
RULE:      ----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+--
591        01AUG2022:11:51:35
datetime=. _ERROR_=1 _N_=1
NOTE: Invalid data for datetime in line 592 1-18.
592        22JUL2019:11:41:32
datetime=. _ERROR_=1 _N_=2
NOTE: Invalid data for datetime in line 593 1-18.
593        22JUL2019:11:50:32
datetime=. _ERROR_=1 _N_=3
NOTE: Invalid data for datetime in line 594 1-18.
594        25FEB2015:16:05:39
datetime=. _ERROR_=1 _N_=4
NOTE: The data set WORK.HAVE has 4 observations and 1 variables.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds

If you want the year to appear with 4 digits you need to set a longer than default width for the Datetime format.

data have;
input datetime :datetime.;
format datetime datetime19.;
infile datalines missover;
datalines;
01AUG2022:11:51:35
22JUL2019:11:41:32
22JUL2019:11:50:32
25FEB2015:16:05:39
;
run;

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 lock in 2025 pricing—just $495!

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
  • 3 replies
  • 1610 views
  • 0 likes
  • 3 in conversation