BookmarkSubscribeRSS Feed
Akshayvrata
Calcite | Level 5

Hi,

I have to perform the hour function on a variable in which observations display like (515, 815, etc.) though it has to be like (5:15, 8:15, etc.). Now how can I insert colon in my observations so that they appear like later ones. 

i know that i have to change the format of the variable to character then only I can add colon.   

 

So please tell me what function I can use to solve this?

 

Thanks! 

2 REPLIES 2
snoopy369
Barite | Level 11

So you have a numeric field "515" (five hundred fifteen), and it represents 5 hours 15 minutes? I would split this off like so:


data want; timevar=515; minutes = mod(timevar, 100); hours = floor(divide(timevar,100)); final_var = hms(hours,minutes,0); format final_var hhmm5.; run;

This makes it a numeric date variable displaying correctly.  But you could also use PUT to make this character.

Reeza
Super User

Use SUBSTR() to get the last two characters and then SUBSTR() to get the first or first two characters.

If the length is less than 4 it may be worth adding the leading zero. 

Do you want another character variable though, or a SAS time? 

 

    data want;
    input have $;
    if length(have) < 4 then have = catt('0', have);
    hours = substr(have, 1, 2);
    minutes = substr(have, 3, 2);
    time_char = catx(":", hours, minutes);
    time_sas = input(time_char, time.);
    format time_sas time.;
    cards;
    423
    1423
    830
    1204
    ;
    run;

    proc print data=want;
    run;

@Akshayvrata wrote:

Hi,

I have to perform the hour function on a variable in which observations display like (515, 815, etc.) though it has to be like (5:15, 8:15, etc.). Now how can I insert colon in my observations so that they appear like later ones. 

i know that i have to change the format of the variable to character then only I can add colon.   

 

So please tell me what function I can use to solve this?

 

Thanks! 


 

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 1415 views
  • 3 likes
  • 3 in conversation