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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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