BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
eduard1231
Fluorite | Level 6

I have following input format HHMMSSXXX, for example 93552085, I want to transform it into time format like 9:35:52,085.

Thanks! 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

@PaigeMiller wrote:

@eduard1231 wrote:

I have following input format HHMMSSXXX, for example 93552085, I want to transform it into time format like 9:35:52,085.

Thanks! 


You can't apply a time format such as HHMMSS. unless you have a time value, which is the number of seconds after midnight. You do not have a time value, since this is a lot more than 24 hours after midnight, in seconds.

 

Thus, you have to convert this to an actual SAS time value using an informat such as B8601TMw.d

 

UNTESTED CODE, assuming you have a numeric value of 93552085

 

data want;
    time=input(93552085,b8601tm8.3);
    format time E8601TM12.3;
run;

Great find. Looks like the strings should have been 9 characters in length to begin with, so need 9 as the width on the informat.

If the value is a number that convert it to string first before trying to use INPUT, use Z format as that informat needs the extra zero when the hour part is less than 10.

Here is the cleaned up code assuming the existing variable is already named RAWTIME.  Note that TIME values are numbers so you can re-use the same variable if you want.

 time=input(put(RAWTIME,Z9.),b8601tm9.3);

 

View solution in original post

10 REPLIES 10
novinosrin
Tourmaline | Level 20

Hi @eduard1231   What do you mean by input format HHMMSSXXX?

 

1. Do you have data as raw data/external file or SAS datasaet?

2. Is your time value stored as character or numeric just numbers?

 

You could explain the details more clearly plz

eduard1231
Fluorite | Level 6

Input is numeric, no leading zeros, if it is 9am even, my number will be 90000000, which is HMMSSXXX, if it is 10am even, it is going to be 100000000, which is HHMMSSXXX.

eduard1231
Fluorite | Level 6

1. Dataset is in SAS format

2. Numeric

ed_sas_member
Meteorite | Level 14

Hi @eduard1231 

Could you please specify the type of the input variable? (Character or numeric?)

Are their leading zeros before minutes, etc. ? For example, would you have 100452085 for 10:04:52,085, or 10452085?

In this case, how would you differentiate it from 1:04:52,085?

 

Best,

 

eduard1231
Fluorite | Level 6

Input is numeric, no leading zeros, if it is 9am even, my number will be 90000000, which is HMMSSXXX, if it is 10am even, it is going to be 100000000, which is HHMMSSXXX.

Tom
Super User Tom
Super User

You can use a decimal part on the informat to have SAS treat the last three digits as the decimal part.  If you already have it as an integer number just divide by 1E3 (aka 10**3 aka 1000).

But to convert the integer part into hours,minutes and seconds you will need to do more work. 

Here are two ways. 

One is to split the digits into three parts.  You could use math.  (Or substr() on the string)

The other is to make a picture format that displays the number with colons in the right places and then read that output as a time value.

proc format ;
picture as_time
 low-high = '99:99:99.999' 
;
run;
data test;
  str='93552085';
  num=input(str,10.3);
  num2=num;
  time1=hms(int(num/1E4),int(mod(num,1E4)/100),mod(num,100));
  time2=input(put(num,as_time.),time12.);
  format num comma12.3 num2 as_time. time: time12.3;
  put (_all_) (=/);
run;
str=93552085
num=93,552.085
num2=09:35:52.085
time1=9:35:52.085
time2=9:35:52.085

 

PaigeMiller
Diamond | Level 26

@eduard1231 wrote:

I have following input format HHMMSSXXX, for example 93552085, I want to transform it into time format like 9:35:52,085.

Thanks! 


You can't apply a time format such as HHMMSS. unless you have a time value, which is the number of seconds after midnight. You do not have a time value, since this is a lot more than 24 hours after midnight, in seconds.

 

Thus, you have to convert this to an actual SAS time value using an informat such as B8601TMw.d

 

UNTESTED CODE, assuming you have a numeric value of 93552085

 

data want;
    time=input(93552085,b8601tm8.3);
    format time E8601TM12.3;
run;
--
Paige Miller
Tom
Super User Tom
Super User

@PaigeMiller wrote:

@eduard1231 wrote:

I have following input format HHMMSSXXX, for example 93552085, I want to transform it into time format like 9:35:52,085.

Thanks! 


You can't apply a time format such as HHMMSS. unless you have a time value, which is the number of seconds after midnight. You do not have a time value, since this is a lot more than 24 hours after midnight, in seconds.

 

Thus, you have to convert this to an actual SAS time value using an informat such as B8601TMw.d

 

UNTESTED CODE, assuming you have a numeric value of 93552085

 

data want;
    time=input(93552085,b8601tm8.3);
    format time E8601TM12.3;
run;

Great find. Looks like the strings should have been 9 characters in length to begin with, so need 9 as the width on the informat.

If the value is a number that convert it to string first before trying to use INPUT, use Z format as that informat needs the extra zero when the hour part is less than 10.

Here is the cleaned up code assuming the existing variable is already named RAWTIME.  Note that TIME values are numbers so you can re-use the same variable if you want.

 time=input(put(RAWTIME,Z9.),b8601tm9.3);

 

eduard1231
Fluorite | Level 6

Thanks! That is what I wanted!

eduard1231
Fluorite | Level 6

Hi again,

 

I have the same issue as before, but now I have hhmmssxxxxxxxxx format, so basically I have data timestamped to nanoseconds. I tried to adapt your suggested code, but as far as I understood, this time format provides up to 6 decimals after seconds. That is what I tried:

 

time=12345612312312;

time_m=input(put(time,Z15.),b8601tm15.9);

format time_m E8601TM15.9;

 

This gives me following error:

time_m=input(put(time,Z14.),b8601tm14.8); - Decimal specified for informat B8601TM is invalid.

format time_m E8601TM14.8 - Decimal specified for format E8601TM is invalid.

 

Thanks!

Ed

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!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 10 replies
  • 2703 views
  • 3 likes
  • 5 in conversation