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

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 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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

7 REPLIES 7
PGStats
Opal | Level 21

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
Diana_Alfaro
Calcite | Level 5

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

 

PGStats
Opal | Level 21

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
Reeza
Super User

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



 

Diana_Alfaro
Calcite | Level 5
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.
Reeza
Super User

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.

 

Kurt_Bremser
Super User

@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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 7 replies
  • 7084 views
  • 1 like
  • 4 in conversation