hi
i am tring to conver a charecter field popelated with (2018-09-01 00:00:00) to datetime19. format .
as i have used the below script but it give me the output of only dates without time:
data want (drop=TRANSACTION_DATE _date );
set b;
format TRANSACTION_DATE_1 YYMMDD10. ;
informat TRANSACTION_DATE_1 YYMMDD10. ;
_date=substr(TRANSACTION_DATE,1,18);
TRANSACTION_DATE_1=INPUT(_date,YYMMDD10.);
run;
data want2 (drop=TRANSACTION_DATE_ );
set want;
format TRANSACTION_DATE YYMMDD10. ;
informat TRANSACTION_DATE YYMMDD10.;
TRANSACTION_DATE=TRANSACTION_DATE_1;
run;
Thank You
Hi @YousefOba and welcome to the SAS Support Communities!
So, your dataset b contains a character variable TRANSACTION_DATE of length 21 with datetime values of the form
yyyy-mm-dd hh:mm:ss
Actually, length 19 would be sufficient for such values. As a consequence, there will be two additional blanks (most likely) in these strings. Let's make no assumption about the position of these blanks.
Example data:
data b;
length transaction_date $21;
input transaction_date $char21.;
cards;
2018-09-01 01:00:00
2018-09-01 02:00:00
2018-09-01 03:00:00
2018-09-01 04:00:00
2018-09-01 05:00:00
2018-09-01 06:00:00
;
You can use the ANYDTDTM21. informat to convert these character values into SAS datetime values and then apply the DATETIME19. format to the resulting numbers:
data want(drop=transaction_date);
set b;
tadt=input(transaction_date, anydtdtm21.);
format tadt datetime19.;
run;
Hi @YousefOba and welcome to the SAS Support Communities!
So, your dataset b contains a character variable TRANSACTION_DATE of length 21 with datetime values of the form
yyyy-mm-dd hh:mm:ss
Actually, length 19 would be sufficient for such values. As a consequence, there will be two additional blanks (most likely) in these strings. Let's make no assumption about the position of these blanks.
Example data:
data b;
length transaction_date $21;
input transaction_date $char21.;
cards;
2018-09-01 01:00:00
2018-09-01 02:00:00
2018-09-01 03:00:00
2018-09-01 04:00:00
2018-09-01 05:00:00
2018-09-01 06:00:00
;
You can use the ANYDTDTM21. informat to convert these character values into SAS datetime values and then apply the DATETIME19. format to the resulting numbers:
data want(drop=transaction_date);
set b;
tadt=input(transaction_date, anydtdtm21.);
format tadt datetime19.;
run;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.