DATA Step, Macro, Functions and more

Converting SAS Character time to SAS time, minutes wrong

Reply
Frequent Contributor
Posts: 78

Converting SAS Character time to SAS time, minutes wrong

I'm using the following code and never had a problem before, but the minutes are not always correct.

the original time variable data looks like "2030":

delhr=SUBSTR(Del_time, 1,2);
delmin=SUBSTR(Del_time, 3,2);
del_time= trim(delhr)||':'||trim(delmin);


format DELTIME time5.;
DELTIME=input(del_time,time5.); Message was edited by: statadm
Super Contributor
Super Contributor
Posts: 3,174

Re: Converting SAS Character time to SAS time, minutes wrong

I'd say that you're going to need to demonstrate the "symptom condition" by pasting some of a SAS log, where the time-variable assigned is incorrect as you have explained. You can use a PUT statement in your DATA step, however since precision is a consideration (the LENGTH attribute of your SAS numeric variable), you will need to also demonstrate the problem likely with a follow-on PROC PRINT or another DATA step where you input the same, just-created SAS file, and then do another PUT. SAS stores these numeric variables in floating-point until saved to the SAS library -- that's why doing a PUT in the "input processing" DATA step is not going to be useful to debug -- of course unless you have some true INPUT logic or input-data problems.

Scott Barry
Frequent Contributor
Posts: 78

Re: Converting SAS Character time to SAS time, minutes wrong

After taking a break and coming back to it, I realized it was just a matter of SAS not liking the use of the same name for the variable. It works correctly if I rename the variable to del_time1 instead of overwriting the character variable with numeric data.

Thanks for your suggestion.
Super Contributor
Super Contributor
Posts: 3,174

Re: Converting SAS Character time to SAS time, minutes wrong

From what you posted, there are different names for the two SAS CHARACTER and NUMERIC variables. SAS would not permit you to interchange one for the other without some warning or error diagnostic.

Scott
Frequent Contributor
Posts: 78

Re: Converting SAS Character time to SAS time, minutes wrong

I changed the original program:

delhr=SUBSTR(Del_time, 1,2);
delmin=SUBSTR(Del_time, 3,2);
del_time= trim(delhr)||':'||trim(delmin);


format DELTIME time5.;
DELTIME=input(del_time,time5.);

To read as follows:

delhr=SUBSTR(Del_time, 1,2);
delmin=SUBSTR(Del_time, 3,2);
del_time1= trim(delhr)||':'||trim(delmin);


format DELTIME time5.;
DELTIME=input(del_time1,time5.);

I'm sorry, I mean that it didn't like me using the same variable name for the original variable and the new character variable with the ":". Once I changed this, it worked fine.
Trusted Advisor
Posts: 2,116

Re: Converting SAS Character time to SAS time, minutes wrong

Reading the thread, I have a strong suspicion that the underlying cause of the problem is not that SAS doesn't like reusing a variable name (that's fine), but that the original del_time was character length=4 and the assignment truncated the minutes in the assignment statement

del_time= trim(delhr)||':'||trim(delmin);

when the original value was "1000" or greater.
Ask a Question
Discussion stats
  • 5 replies
  • 493 views
  • 0 likes
  • 3 in conversation