DATA Step, Macro, Functions and more

convert the numeric variable in time

Accepted Solution Solved
Reply
New Contributor
Posts: 3
Accepted Solution

convert the numeric variable in time

Hi all,

 

I have the numeric variable time with e.g. the values 819.00, 1445.00, 2159.00, ... . Those values should be converted to a SAS time format representing the clock time hh:mm:ss am/pm e.g. 8:19:00 am, 14:45:00 am, 21:59:00 pm.

 

Could you help me to figure out the code?

 

Thanks

Diana


Accepted Solutions
Solution
‎11-27-2017 03:13 PM
Super User
Posts: 23,980

Re: convert the numeric variable in time

[ Edited ]
Posted in reply to Diana_Alfaro

Use PUT to convert it to a text field

Use INPUT to read it correctly as a time field using the informat, hhmmss.

 

 

 

data from_text;
informat time_old hhmmss.;
format time_old time.;
input time_old;
cards;
819
1445
2159
;
run;


data have_diff;
input num;
cards;
819
1445
2159
;
run;


data want;
set have_diff;
x = input(put(num, 8.), hhmmss.);
format x time.;
run;

title 'results from reading in as text file';
proc print data=from_text;
title 'results from using input, where data already exists';
proc print data=want;
run;

 

 


Diana_Alfaro wrote:

Hi all,

 

I have the numeric variable time with e.g. the values 819.00, 1445.00, 2159.00, ... . Those values should be converted to a SAS time format representing the clock time hh:mm:ss am/pm e.g. 8:19:00 am, 14:45:00 am, 21:59:00 pm.

 

Could you help me to figure out the code?

 

Thanks

Diana



 

View solution in original post


All Replies
Esteemed Advisor
Posts: 5,620

Re: convert the numeric variable in time

Posted in reply to Diana_Alfaro

HMS function and basic math:

 

data test;
input timeN @@;
time = hms(floor(timeN/100), mod(timeN,100), 0);
format time time8.;
datalines;
819.00 1445.00 2159.00
;

proc print; run;
PG
New Contributor
Posts: 3

Re: convert the numeric variable in time

It's not working

 

 

data Data_Base5;

set Data_Base4;

input Time_Def @@;

format Time Time8.;

Time_def=hms(floor(Time/100), mod(Time,100), 0);

Proc print;

RUN;

 

ERROR: No. DATALINES or INFILE statement

 

Esteemed Advisor
Posts: 5,620

Re: convert the numeric variable in time

Posted in reply to Diana_Alfaro

Fool proof:

 

data Data_Base5;
set Data_Base4;
format Time_def Time8.;
Time_def = hms(floor(input(cats(Time),best.)/100), mod(input(cats(Time),best.),100), 0);
run;

Proc print;
RUN;
PG
Solution
‎11-27-2017 03:13 PM
Super User
Posts: 23,980

Re: convert the numeric variable in time

[ Edited ]
Posted in reply to Diana_Alfaro

Use PUT to convert it to a text field

Use INPUT to read it correctly as a time field using the informat, hhmmss.

 

 

 

data from_text;
informat time_old hhmmss.;
format time_old time.;
input time_old;
cards;
819
1445
2159
;
run;


data have_diff;
input num;
cards;
819
1445
2159
;
run;


data want;
set have_diff;
x = input(put(num, 8.), hhmmss.);
format x time.;
run;

title 'results from reading in as text file';
proc print data=from_text;
title 'results from using input, where data already exists';
proc print data=want;
run;

 

 


Diana_Alfaro wrote:

Hi all,

 

I have the numeric variable time with e.g. the values 819.00, 1445.00, 2159.00, ... . Those values should be converted to a SAS time format representing the clock time hh:mm:ss am/pm e.g. 8:19:00 am, 14:45:00 am, 21:59:00 pm.

 

Could you help me to figure out the code?

 

Thanks

Diana



 

New Contributor
Posts: 3

Re: convert the numeric variable in time

it's not working
Error
1. Informat $HHMMSS was not found or could not be loaded.
2. Format $TIME was not found or could not be loaded.
Super User
Posts: 23,980

Re: convert the numeric variable in time

Posted in reply to Diana_Alfaro

1. Please confirm that your variable is numeric. Are you tring to implement this on data you already have in SAS or during an import step?

2. Saying something works isn't helpful. Please show your full code and log. Mine solution includes a fully worked example that first imports a set of sample data and then converts it. I would compare it against your data/code and see how it differs, that will tell you where your mistake is.

 


Diana_Alfaro wrote:
it's not working
Error
1. Informat $HHMMSS was not found or could not be loaded.
2. Format $TIME was not found or could not be loaded.

 

Super User
Posts: 10,534

Re: convert the numeric variable in time

Posted in reply to Diana_Alfaro

Diana_Alfaro wrote:
it's not working
Error
1. Informat $HHMMSS was not found or could not be loaded.
2. Format $TIME was not found or could not be loaded.

Which means that your time variable is NOT numeric, but character. It just looks like a number. Verify that.

 

This is one of the reasons why we ask people to post their example datasets in a data step, so we can recreate their data as is.

 

Use the macro provided in https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... to convert your dataset to a data step, and post that here according to the advice in https://communities.sas.com/t5/help/faqpage/faq-category-id/posting?nobounce.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 7 replies
  • 565 views
  • 1 like
  • 4 in conversation