DATA Step, Macro, Functions and more

fixing time date value in a character string

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 142
Accepted Solution

fixing time date value in a character string

[ Edited ]

Hi!  I have compressed date/time variables in a character string with values that now look like this:

 

 

312140AUG07

311415MAY08

092105MAR07

 

The first two numbers are the day(date). 3-6 is the time.  THen month and year....

 

I would like it to look like this format DDMMMYY:HHMM

 

What I am doing is not working.  Appreciate your help!!

 

 

data rr.datetime2006 (keep=Inc Alert Launch OnSce Locate Recov Deliv RTB);
    set rr2006labels;
        array change _character_;
        do over change;
        change=compress(change,'');
        day=substr(change,1,2);
        time=substr(change,3,4);
        month=substr(change,7,3);
        year=substr (10,2);
            date=catt(day,month,year);
            change=catt(date,time);  *want this to look like DDMMMYY:HHMM;
end;
    run;


Accepted Solutions
Solution
‎07-27-2017 02:26 PM
Super User
Posts: 5,495

Re: fixing time date value in a character string

Presumably, you have thought this through and it would be satisfactory to keep the time of day but get rid of the day itself.  In any case, you need just one statement in your DO OVER loop:

 

change = substr(change, 7, 5) || ':' || substr(change, 3, 4);

 

 

View solution in original post


All Replies
Solution
‎07-27-2017 02:26 PM
Super User
Posts: 5,495

Re: fixing time date value in a character string

Presumably, you have thought this through and it would be satisfactory to keep the time of day but get rid of the day itself.  In any case, you need just one statement in your DO OVER loop:

 

change = substr(change, 7, 5) || ':' || substr(change, 3, 4);

 

 

Frequent Contributor
Posts: 142

Re: fixing time date value in a character string

Posted in reply to Astounding

@astounding  I do need to keep the day in this.  I see what your saying though - correction I need the end format to be DDMMMYY:HHMM.  But even removing the day- i received an error in your code (Invalid third argument to function SUBSTR)

Frequent Contributor
Posts: 142

Re: fixing time date value in a character string

Posted in reply to Astounding
Hi @Astounding . So keeping the day, I tried this and got an error:

data rr.datetime2006 (keep=Inc Alert Launch OnSce Locate Recov Deliv RTB);
set rr2006labels;
array change _character_;
do over change;
change=compress(change,'');
change=substr(change,1,2)||substr(change,7,5)||':'||substr (change,3,4);
end;
run;
Super User
Posts: 5,495

Re: fixing time date value in a character string

The code is correct.  I suspect that _CHARACTER_ is incorrect and includes some character variables that contain fewer than 12 characters.  If that's the case, you might have to replace _CHARACTER_ with a list of just the appropriate variable names.

Frequent Contributor
Posts: 142

Re: fixing time date value in a character string

Posted in reply to Astounding
@Astounding. you were correct. That worked perfectly! Thank you!
Regular Contributor
Regular Contributor
Posts: 156

Re: fixing time date value in a character string

year=substr (change,10,2);
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 171 views
  • 0 likes
  • 3 in conversation