Pattern matching might be justified if the dates are embedded in text:
data test;
dateStr = "To replace dates such as 2016-06-26T20:44:16 in text.";
date10 = prxchange("s#(\d{4})-(\d{2})-(\d{2})T[0-9:]{8}#\2/\3/\1#o", 1, dateStr);
put _all_;
run;
Why PRX?
This is a standard format that you can use with INPUT and DatePart? And why is the date 27, not 26?
data want;
date_char='2016-06-26T20:44:16';
datetime_num=input(date_char, e8601dt.);
format datetime_num datetime20.;
date_want_num=datepart(datetime_num);
format date_want_num mmddyy10.;
date_want_char=put(date_want_num, mmddyys10.);
run;
proc print;run;
There is no reason to use prxparse unless you want to cause yourself more work. Use the proper format when reading the data.
To read that value it looks like you want to use an E8601DT. informat which will result in a SAS datetime value.
The question remains is do want to change the value of variable to only contain the date value or do you want it to stay as a datetime but only display the date portion?
Is your current variable a datetime or character variable?
Or do you just want a character variable but not an actual date variable?
There's probably at least a dozen ways to do this depending on you current value. If the value is character you could create a SAS date valued variable with in a datastep:
date = input(substr(longdatetime,1,10),yymmdd10.) ;
format date mmddyy10.
If the variable is currently a datetime with an E8610 format then
date=datepart(longdatetime);
format date mmddyy10.
Pattern matching might be justified if the dates are embedded in text:
data test;
dateStr = "To replace dates such as 2016-06-26T20:44:16 in text.";
date10 = prxchange("s#(\d{4})-(\d{2})-(\d{2})T[0-9:]{8}#\2/\3/\1#o", 1, dateStr);
put _all_;
run;
data T;
A='2016-06-26T20:44:16';
B=input(A,anydtdte10.);
putlog B mmddyy.;
run;
06/26/16
@Beto16 wrote:
Hi PgStats,
the code works it ran without errors but didn't bring date. It brought in the 1st 199 character from the text box.. I noticed the date is formatted the same way in all my entries "".2016-06-07T00:16:11,, thanks for your assistance
Are you now saying that the data field starts with quotes and has a period before the value?
This will extract the date only, in mmddyy10. format.
data test;
dateStr = "To extract dates such as 2016-06-26T20:44:16 from almost any text.";
date10 = prxchange("s#.*(\d{4})-(\d\d)-(\d\d)T[0-9:]{8}.*#\2/\3/\1#o", 1, dateStr);
put dateStr = / date10 = ;
run;
Well then use SUBSTR to extract the relevant char portion and use any of the above solutions.
SUBSTR is a fairly self explanatory function.
You can make a custom regex informat:
proc format;
invalue anynum (default=24)
's/.*? (?# lazy match of any character )
( (?# 1st capture group )
\d{4}- (?# 4 digits [year] )
\d{2}- (?# 2 digits [month] )
\d{2} (?# 2 digits [day] )
) (?# close 1st capture group )
.*? (?# lazy match of any character )
/\1/x' %* keep date. type text ;
(regexpe)= [yymmdd10.]
other = .;
run;
data _null_;
input X anynum.;
putlog 'Date= ' X date9. ;
cards;
".2016-06-07T00:16:11
run;
Date= 07JUN2016
Simplified from multi-regex informat example taken from:
https://www.amazon.com/High-Performance-SAS-Coding-Christian-Graffeuille/dp/1512397490
No where in your previous comments does it say you were dealing with text in a comment field. Please take the time to describe your problem so the appropriate solution can be suggested.
@Beto16 wrote:
The "".2016-06-07T00:16:11,, is not in the same location in the comment box it varies ...would substr still work ?
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.