Learning SAS? Welcome to the exclusive online community for all SAS learners.

time8. informat does not read hh:mm:ss correctly

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 8
Accepted Solution

time8. informat does not read hh:mm:ss correctly

I am reading "Exercise and Project for the little SAS book" and  can't figure out the solution for Chapter 3 Question 34 .

 

The following is data step I wrote.  Hope someone can help. Thanks.

 

data test;
infile cards dlm=',';

input id (time1 time2 time3 ) (time8.);
forma time1-time3 time8.;
cards;
200,8:54:37,7:48:29,6:12:13
201,9:33:08,7:23:59,9:33:22
202,10:21:30,12:55:29,10:47:46
run;

 

the output doesn't show time properly. 

id       time1     time2     time3

200      .             .           6:12:13

201      .             .           9:33:22

202  10:21:30    .             .

 

the following is log content :

 

NOTE: Invalid data for time1 in line 79 5-12.
NOTE: Invalid data for time2 in line 79 13-20.
Rule: ----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0
79 200,8:54:37,7:48:29,6:12:13
id=200 time1=. time2=. time3=6:12:13 _ERROR_=1 _N_=1

 

if I use stimer8. to replace time8. but the result is still incorrect.(time for id 202 is wrong)

data test;
infile cards dlm=',';
input id (time1 - time3 ) (stimer8.);
format time1-time3 time8.;
cards;
200,8:54:37,7:48:29,6:12:13
201,9:33:08,7:23:59,9:33:22
202,10:21:30,12:55:29,10:47:46
run;

 

the output :

id     time1          time2      time3

200 8:54:37    7:48:29      6:12:13
201 9:33:08    7:23:59      9:33:22
202 10:21:30  0:55:02     9:47:00

 

 

 

Accepted Solutions
Solution
Monday
Super User
Posts: 10,571

Re: time8. informat does not read hh:mm:ss correctly

Posted in reply to mingjueca

By not using the colon modifier for the informat in your input statement, you direct SAS to do formatted input. Formatted input ignores the delimiters and always reads as much bytes as the format requires.

Try

input id (time1 time2 time3) (:time8.);
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code

View solution in original post


All Replies
Super User
Posts: 24,004

Re: time8. informat does not read hh:mm:ss correctly

Posted in reply to mingjueca

It works better if you use an INFORMAT statement. 

 

data test;
infile cards dlm=',';
informat id $8. time1-time3 time8.;
input id $ time1  time2 time3 ;
format time1 time2 time3 time8.;
cards;
200,08:54:37,07:48:29,06:12:13
201,9:33:08,7:23:59,9:33:22
202,10:21:30,12:55:29,10:47:46
;;;;
run;

@mingjueca wrote:

I am reading "Exercise and Project for the little SAS book" and  can't figure out the solution for Chapter 3 Question 34 .

 

The following is data step I wrote.  Hope someone can help. Thanks.

 

data test;
infile cards dlm=',';

input id (time1 time2 time3 ) (time8.);
forma time1-time3 time8.;
cards;
200,8:54:37,7:48:29,6:12:13
201,9:33:08,7:23:59,9:33:22
202,10:21:30,12:55:29,10:47:46
run;

 

the output doesn't show time properly. 

id       time1     time2     time3

200      .             .           6:12:13

201      .             .           9:33:22

202  10:21:30    .             .

 

the following is log content :

 

NOTE: Invalid data for time1 in line 79 5-12.
NOTE: Invalid data for time2 in line 79 13-20.
Rule: ----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0
79 200,8:54:37,7:48:29,6:12:13
id=200 time1=. time2=. time3=6:12:13 _ERROR_=1 _N_=1

 

if I use stimer8. to replace time8. but the result is still incorrect.(time for id 202 is wrong)

data test;
infile cards dlm=',';
input id (time1 - time3 ) (stimer8.);
format time1-time3 time8.;
cards;
200,8:54:37,7:48:29,6:12:13
201,9:33:08,7:23:59,9:33:22
202,10:21:30,12:55:29,10:47:46
run;

 

the output :

id     time1          time2      time3

200 8:54:37    7:48:29      6:12:13
201 9:33:08    7:23:59      9:33:22
202 10:21:30  0:55:02     9:47:00

 

 

 

 

Occasional Contributor
Posts: 8

Re: time8. informat does not read hh:mm:ss correctly

Thanks for your quick response.

 

It works.  But I am curious why my way didn't work . most examples do not use an INFORMAT statement. 

 

 

 

Solution
Monday
Super User
Posts: 10,571

Re: time8. informat does not read hh:mm:ss correctly

Posted in reply to mingjueca

By not using the colon modifier for the informat in your input statement, you direct SAS to do formatted input. Formatted input ignores the delimiters and always reads as much bytes as the format requires.

Try

input id (time1 time2 time3) (:time8.);
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Occasional Contributor
Posts: 8

Re: time8. informat does not read hh:mm:ss correctly

Posted in reply to KurtBremser
Thanks, now I understand.
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 106 views
  • 1 like
  • 3 in conversation