BookmarkSubscribeRSS Feed
Sowmya12
Fluorite | Level 6
Suppose if the values are 0900 and 0930 I need the output as 09:00 and 09:30
Suggest me a solution
6 REPLIES 6
PaigeMiller
Diamond | Level 26

Could you provide more information? Is the variable that contains 0900 and 0930 numeric or character? (I'm guessing character)

 

Does 0900 represent 9 minutes and zero seconds, or is it 9 hours and 0 minutes?

--
Paige Miller
Sowmya12
Fluorite | Level 6
Numeric
PaigeMiller
Diamond | Level 26

Numeric variables don't have leading zeros unless they are formatted. Can you please run PROC CONTENTS and confirm this variable is numeric, and tell us what the format is?

 

You didn't answer my other question: 

 

"Does 0900 represent 9 minutes and zero seconds, or is it 9 hours and 0 minutes?"

--
Paige Miller
Sowmya12
Fluorite | Level 6
Hii

Id time$
1 0900
2 0930
3 0825


The time variable is in character and the output should be in time5.
Format.

Thanks
Sowmya
andreas_lds
Jade | Level 19

@Sowmya12 wrote:
Hii

Id time$
1 0900
2 0930
3 0825


The time variable is in character and the output should be in time5.
Format.

Thanks
Sowmya

Untested:

 

data want;
  set have;
  length time 8 helper $ 5;
  format time time5.;
  helper = cats(substr(timeStr, 1, 2), ':', substr(timeStr, 3, 2));
  time = input(helper, time.);
run;

 

 

ballardw
Super User

What do you have now? Is that "0900" a character value? A number? What is the name of the variable?

 

If character you create a new variable that holds a SAS time value and assign a format: One way:

data example;
  x='0900';
  numval = input(x,f4.);
  timevalue = hms(int(numval/100), mod(numval,100),0);
  format timevalue time5.;
run;

The function hms needs a numeric hour, minute and second value. Since you don't have any seconds shown then that uses 0.

If your current variable is numeric then skip the step that creates Numval.

The format shows values in HH:MM format.

You could drop Numval from the data.

If your variable is a number you could use the name in both the places numval appears in the HMS function and the Timevalue variable.

 

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