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
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
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;
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
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;
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
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.
@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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.