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

Yes, you are right, it was converted wrong, the year should be 2017, that is where I was stopped   

 

usubjidaestdtcold_datenew_date
10012017-03-0217APR04:15:54:0517APR2004:15:54:05.00
10012017-03-0317APR04:15:56:0917APR2004:15:56:09.00
10022017-03-0117APR05:15:36:4817APR2005:15:36:48.00
ScottBass
Rhodochrosite | Level 12

@zimcom wrote:

Yes, you are right, it was converted wrong, the year should be 2017, that is where I was stopped   

 

Well that would be a different problem then.  Good luck, unsubscribing from this one...


Please post your question as a self-contained data step in the form of "have" (source) and "want" (desired results).
I won't contribute to your post if I can't cut-and-paste your syntactically correct code into SAS.
zimcom
Pyrite | Level 9

@ScottBass 

 

Thank you, Scott for your help!

Patrick
Opal | Level 21

@zimcom 

Your source data is a string in the form YYMONDD:HH:MM:SS

You used informat ANYDTDTM40. to read this string and convert it to a SAS DateTime value (=numerical value; count of seconds since 1/1/1960).

 

So now by using ANYDTDTM40. SAS will try to read the text string as DDMONYY:HH:MM:SS - and because the text string (your data) also represents a valid datetime in this form SAS will use it and convert it to the number of seconds since 1/1/1960. Now you have a SAS DataTime value and whatever operations and formats you use on this value it's always against the "wrong" count of seconds.

-> your current code gets it wrong when converting the source string into a SAS DateTime value.

 

Others wrote this already but here again:

What you need to do is first to read the string into a SAS character variable and then "reshuffle" it into a form which a SAS datetime informat can interprete the way you need it. Below a code sample illustrating how this could work (for the date portion of the string).

/* read the excel sheet into SAS */
PROC IMPORT 
  OUT= source 
  DATAFILE= "c:\temp\Convert dates.xlsx" 
  DBMS=xlsx 
  REPLACE;
  SHEET="#LN00046";
  GETNAMES=YES;
RUN;

/*18DEC13:11:45:31*/
/* 
  extract the date string portion from column date_old
  and convert it to a SAS date value (number of seconds since 1/1/1960
  DT string is in the form: YYMONDD:HH:MM:SS
*/
data want;
  set source;
  length dt_string $7;
  format dt_SASDateValue is8601da.;
  dt_string       =cats(substrn(date_old,6,2),substrn(date_old,3,3),substrn(date_old,1,2));
  dt_SASDateValue =input(dt_string,?? date7.);
run;

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 18 replies
  • 11322 views
  • 4 likes
  • 5 in conversation