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

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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