BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Kc2
Quartz | Level 8 Kc2
Quartz | Level 8
Hi,
 
 
 
I need to convert a time variable currently in character toa numeric but the data is non standard format;
 
 
data have ;
input starttm 8.;
datalines;
13.48
9
10
9.3
8.56
11.10
.
1.2
run;
/*
Want the data to look:
13:48
09:00
10:00
09:30
08:56
11:10
.
01:20
*/
 
data want;
length srtm1 srtm3 srtm4_ srtm4 $5; 
set have;
 
 
srtm1=put(starttm,time5.);/* Not working. All observatsion=0:00*/
 
*srtm2=put(starttm,hhmmss5.);* Works only for observations 2,3*/;
 
srtm3=put(starttm,tod5.);/* Not working. All observatsion=0:00*/
 
srtm4_=tranwrd(strip(put(starttm,best5.)),".",":");/* Not working. same as original*/
 
if starttm ^=. then do;
     if index(srtm4_,":")=3 then srtm4= srtm4_;
else if index(srtm4_,":")=2  then srtm4= srtm4_||"0";
else if index(srtm4_,":")=20 then srtm4= srtm4_||":00";
end; 
 
 
if index(srtm4_,":")>0 then srtm5= input(srtm4_,time5.); /* Not working dor observation 4,6*/
else   srtm5= input(srtm4_,hhmmss5.);
 
format srtm5 time5.;
 
run;
 
 
Any suggestions as to how to get a format that works for each value?
 
Thanks.
1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

Two questions to start. You say you have character values but your data step creates the variable starttm as numeric. So is YOUR variable character of numeric?

 

Second, do you want an actual time value or just something character that fakes it? (Strongly recommend actual time values as they can be worked with much easier).

 

The below custom Format will display the example numeric values in the desired appearance:

proc format library=work;
picture colsep 
  low-high='99:99' (decsep=':')
  . = '.'
;
run;

proc print data=have noobs;
  format starttm colsep5.;
run;

View solution in original post

7 REPLIES 7
ballardw
Super User

Two questions to start. You say you have character values but your data step creates the variable starttm as numeric. So is YOUR variable character of numeric?

 

Second, do you want an actual time value or just something character that fakes it? (Strongly recommend actual time values as they can be worked with much easier).

 

The below custom Format will display the example numeric values in the desired appearance:

proc format library=work;
picture colsep 
  low-high='99:99' (decsep=':')
  . = '.'
;
run;

proc print data=have noobs;
  format starttm colsep5.;
run;
Kc2
Quartz | Level 8 Kc2
Quartz | Level 8

I received the data with the time in a numeric decimal format.  I was told that 9.30 represents 9 hours and 30 minutes.

I need to append this to a date variable to get a character variable with the date and time together as a character.

If I want to use the DHMS function I need the time in a standard numeric format, So I thought of converting the time variable to a character, standardizing it, reconverting it to a numeric variable and use it with DHMS.

PaigeMiller
Diamond | Level 26

13.48 becomes 13:48. Is that 13 hours and 48 minutes, or 13 minutes and 48 seconds? How can you reconcile a decimal of 13.48, which is less than 13 1/2 becoming something larger that 13 1/2 (13 min and 48 seconds is larger than 13 1/2 minutes) ????


Explain in more detail what the data is, and what you want to do with it.

--
Paige Miller
Kc2
Quartz | Level 8 Kc2
Quartz | Level 8

I received the data as is. I was told that 9.30 represent 09:30 9 hours and 30 min

I have to create a date time variable that has the date and the time .

 

PaigeMiller
Diamond | Level 26

@Kc2 wrote:

I received the data as is. I was told that 9.30 represent 09:30 9 hours and 30 min

I have to create a date time variable that has the date and the time .

 


But this doesn't answer my other question: is 13.48 really 13 hours and 48 minutes, or does the .48 indicate 0.48 hours which is not 48 minutes?

--
Paige Miller
ballardw
Super User

@Kc2 wrote:

I received the data as is. I was told that 9.30 represent 09:30 9 hours and 30 min

I have to create a date time variable that has the date and the time .

 


This will create the time part of :

proc format library=work;
picture colsep 
  low-high='99:99' (decsep=':')
  . = '.'
;
run;

data want;
  set have;
  timeval = input(put(starttm,colsep5.),time.);
  format timeval time5.;
run;

if you have a date valued SAS variable then to get a datetime you would use:

datetimevar = dhms(datevariable,0,0,timeval);

The DHMS , D=date H=hour M=minute s=second, function is to make datetime values from date, hour, minute and seconds. With a time value you set the H and M to zero and the Timeval has enough seconds for the hour, minute and seconds if there had been any.

Kc2
Quartz | Level 8 Kc2
Quartz | Level 8

thank you @ballardw , You are the best!.

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 7 replies
  • 363 views
  • 3 likes
  • 3 in conversation