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

Hi All,

I have variable I need converted from a number to a time value.  The format now is 15, 30, 45, 100, 115 and I want 12:00AM, 12:15AM, 12:30AM, 12:45AM 1:00AM etc.

Steve

1 ACCEPTED SOLUTION

Accepted Solutions
CTorres
Quartz | Level 8

This is Mark Johnson' solution improved:

data want;

set have;

txt=put(number,z4.);

time = put(input(substr(txt,1,2),2.)*3600+(input(substr(txt,3),2.)*60),timeampm.);

drop txt;

run;

CTorres

View solution in original post

6 REPLIES 6
Ron_MacroMaven
Lapis Lazuli | Level 10

RTFM on the

TIMEAMPMw.d Format

attrib MyTimeVar length = 8 format =timeampm5.2;

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Post some test data (as a datastep).  At a guess:

Step 1 - split the variable and convert to minutes (note the best coding method, but its to show the process):

data want;

     set have;

     i=1;

     do while (scan(variable,i,",") ne "");

          mins=input(scan(variable,i,","),best.);

          if mins=100 then mins=60;

          if mins=115 then mins=75;

          new_time="12:00"t + mins;

          output;

     end;

run;

Steelers_In_DC
Barite | Level 11

Are you sure 100 should be 1:00 am?  You go from 45 minutes to 100 minutes, logically 45 = 12:45am and 100 = 1:00am doesn't jive:

data have;

input number;

cards;

15

30

45

100

115

;

run;

data want;

set have;

time = put(number*60,timeampm.);

run;

Tommywhosc
Obsidian | Level 7

Are you saying that your actual data values are HOUR:MINUTE, but w/o the colon?

0245 (the integer) <=> 02:45 (the time)??

If so, then pull out the hour, minute values, convert to seconds.

(but w/o any data, we're just guessing).

data want; set have;

minutes = mod(badtime,100);

hours = floor(badtime/100);

seconds = hours*3600 + minutes*60;

format seconds timeampm. ;

run;

CTorres
Quartz | Level 8

This is Mark Johnson' solution improved:

data want;

set have;

txt=put(number,z4.);

time = put(input(substr(txt,1,2),2.)*3600+(input(substr(txt,3),2.)*60),timeampm.);

drop txt;

run;

CTorres

BU2B
Calcite | Level 5

Thanks guys!  I'm sure all of them would work, but I got the last one to work first.  Thanks again!

Steve

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 1217 views
  • 6 likes
  • 6 in conversation