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