DATA Step, Macro, Functions and more

How to insert a colon in between already existing observations?

Reply
Regular Learner
Posts: 1

How to insert a colon in between already existing observations?

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! 

Super Contributor
Posts: 320

Re: How to insert a colon in between already existing observations?

Posted in reply to Akshayvrata

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.

Super User
Posts: 22,874

Re: How to insert a colon in between already existing observations?

Posted in reply to Akshayvrata

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! 


 

Ask a Question
Discussion stats
  • 2 replies
  • 73 views
  • 3 likes
  • 3 in conversation