BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
jenim514
Pyrite | Level 9

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;

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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

6 REPLIES 6
Astounding
PROC Star

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);

 

 

jenim514
Pyrite | Level 9

@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)

jenim514
Pyrite | Level 9
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;
Astounding
PROC Star

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.

jenim514
Pyrite | Level 9
@Astounding. you were correct. That worked perfectly! Thank you!
HB
Barite | Level 11 HB
Barite | Level 11
year=substr (change,10,2);

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
  • 6 replies
  • 864 views
  • 0 likes
  • 3 in conversation