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

I've tried various things but I'm not able to add a leading zero to timepart after converting it to character. If the timepart is 9:29:40 I want it to be 09:29:40. I thought using length would work but it turns out that even though nothing appears to be in the first slot, length still counts something as being there.

 

Thanks

 

data table1;
infile datalines;
input did pgm_id user $ dttime DATETIME24.3; 
format dttime DATETIME24.3;
return;
datalines;
8 63832680 DAN 23AUG2013:19:42:55.160
2 63832680 JAN 01AUG2013:19:20:58.785
6 63832680 DAN 22AUG2013:08:57:12.506
4 63832680 DAN 13AUG2013:19:29:39.578
7 63832680 DAN 23AUG2013:15:30:00.452
9 63832680 DAN 26AUG2013:07:29:40.386
1 63832680 BOB 14AUG2013:22:24:41.894
5 63832680 DAN 16AUG2013:00:40:52.547
3 63832680 DAN 08AUG2013:05:13:00.356
;
run;

data table2;
	set table1;
	format new_dt yymmdds10.;
	format new_tm time8.;
	format tm_new2 tm_new3  y $8.;
	new_dt=datepart(dttime);
	new_tm=timepart(dttime);
	dt_new1=PUT(new_dt, yymmdds10.);
	tm_new1=PUT(new_tm, time8.);
	var1= PUT(INPUT(new_tm, time8.), z8.);
	if length(tm_new1) = 7 then tm_new2= cat('0',tm_new1);
	else tm_new2=tm_new1;
	
	if length(tm_new1) = 7 then tm_new3= '0' || tm_new1;
	else tm_new3=tm_new1;
	len=length(tm_new1);
	y=translate(tm_new1,' ','&');
run;
1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

Hi @DanD999  Please see if this helps

 

	tm_new1=translate(right(PUT(new_tm, time8.)),'0',' ');

Basically the idea is

1. Move the formatted character value to the right, or in other words, right align the formatted char value.

2.Translate the leading blank(if exists)  with 0

Should be straight forward to understand

 

View solution in original post

7 REPLIES 7
novinosrin
Tourmaline | Level 20

Hi @DanD999  Please see if this helps

 

	tm_new1=translate(right(PUT(new_tm, time8.)),'0',' ');

Basically the idea is

1. Move the formatted character value to the right, or in other words, right align the formatted char value.

2.Translate the leading blank(if exists)  with 0

Should be straight forward to understand

 

Tom
Super User Tom
Super User

Just use a display format that puts in the zero.

195   data _null_;
196    x='08:00't;
197    put x= time8. / x=tod8. ;
198   run;

x=8:00:00
x=08:00:00
DanD999
Quartz | Level 8

data table2;
	set table1;
	format new_dt yymmdds10.;
	format new_tm time8.;
	format tm_new2 tm_new3  y $8.;
	new_dt=datepart(dttime);
	new_tm=timepart(dttime);
	new_tm=time8. / new_tm=tod8. ;
run;
31         	new_tm=time8. / new_tm=tod8. ;
                   ______          _____
                   386             390
                   201             201
ERROR 386-185: Expecting an arithmetic expression
ballardw
Super User

Do not use formats in an attempt to tell SAS how to do calculations other than with INPUT functions creating numeric values from character as part of the calculation.

 

What is this supposed to do?

new_tm=time8. / new_tm=tod8. ;

SAS sees an attempt to assign a value to the variable new_tm with the "value" of time8. which is not a valid variable name or value. So the time8. is not a valid arithmetic expression and throws an error. The code goes on the attempt to divide that non-valid value by the result of comparing new_tm to the value tod8. causing another identical error.

 

The current format of a date, time or datetime variable has no effect on how the value is used in calculations. None.

DanD999
Quartz | Level 8

I wasn't trying to do a calculation originally. This was a solution someone provided. I'd never seen something like and couldn't get it to work. Thanks for the comment.

novinosrin
Tourmaline | Level 20

you missed to add PUT statement in

new_tm=time8. / new_tm=tod8. ;

the syntax.  You could use TOD format instead of time8. format to get the same result

tm_new1=PUT(new_tm, tod8.);

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 7 replies
  • 2680 views
  • 2 likes
  • 4 in conversation