- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
data T;
A='2016-06-26T20:44:16';
B=input(A,anydtdte10.);
putlog B mmddyy.;
run;
06/26/16
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Well then use SUBSTR to extract the relevant char portion and use any of the above solutions.
SUBSTR is a fairly self explanatory function.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
data test;
Date10 = prxchange("s#(\d{4})-(\d{2})-(\d{2})T[0-9:]{8}#\2/\3/\1#o", 1, dateStr); put _all_; run;
It doesn't bring date .. thanks for the help
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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 ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
PRX=prxparse('/(1ZT[A-Z\d]{14}) an where it matches 1ZT Bring in everything in the 10 character to the left of 1ZT ? the date is always in front of the 1ZT your thoughts?
data _null_; file "%sysfunc(pathname(WORK))\t.txt"; X='X. Bacode. N. Newvar ZT2487 '; put X; X='Blah,blah,blah, Atmbarcode :"TY272822. 2'; put X; X='Blah, blah,blah,blah, '; put X; X='1ZT2487A122111112,blah,blah, '; put X; run; data WANT; infile "%sysfunc(pathname(WORK))\t.txt" pad; input X $80.; PRX=prxparse('/(1ZT[A-Z\d]{14}) (?# capture 1ZT and 14 letters or digits) | (?# or) (?<=Atmbarcode\W{3}) (?# capture preceeded by Atmbarcode\W{3} ) (TY\d*) (?# capture TY and digits) /x'); call prxsubstr(PRX,X,POS,LEN); if POS then STR=substr(X,POS,LEN); putlog STR=; run;
STR=
STR=TY272822
STR=