DATA Step, Macro, Functions and more

use prxparse finding date

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 80
Accepted Solution

use prxparse finding date

Hi I'm want to use paxparse to get the date from a csv file. The date is format 2016-06-26T20:44:16 I would want the output to look like this 06/27/2016 ...ThAnka for assistance

Accepted Solutions
Solution
‎10-04-2016 01:32 AM
Respected Advisor
Posts: 4,919

Re: use prxparse finding date

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

View solution in original post


All Replies
Super User
Posts: 19,770

Re: use prxparse finding date

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;

 

 

Super User
Posts: 11,343

Re: use prxparse finding date

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.

 

 

Solution
‎10-04-2016 01:32 AM
Respected Advisor
Posts: 4,919

Re: use prxparse finding date

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;
PG
PROC Star
Posts: 1,759

Re: use prxparse finding date

data T;

A='2016-06-26T20:44:16';

B=input(A,anydtdte10.);

putlog B mmddyy.;

run;

 

06/26/16

 

Frequent Contributor
Posts: 80

Re: use prxparse finding date

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
Super User
Posts: 11,343

Re: use prxparse finding date


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?

Respected Advisor
Posts: 4,919

Re: use prxparse finding date

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;
PG
Frequent Contributor
Posts: 80

Re: use prxparse finding date

Yes that is how all date entries look "".2016-06-07T00:16:11,, thanks for your assistance
Super User
Posts: 19,770

Re: use prxparse finding date

Well then use SUBSTR to extract the relevant char portion and use any of the above solutions. 

 

SUBSTR is a fairly self explanatory function.

Frequent Contributor
Posts: 80

Re: use prxparse finding date

The "".2016-06-07T00:16:11,, is not in the same location in the comment box it varies ...would substr still work ? The prxchange actually comes close to what I need
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

PROC Star
Posts: 1,759

Re: use prxparse finding date

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:

 

High-Performance SAS Coding

 

https://www.amazon.com/High-Performance-SAS-Coding-Christian-Graffeuille/dp/1512397490

 

Super User
Posts: 19,770

Re: use prxparse finding date


Beto16 wrote:
The "".2016-06-07T00:16:11,, is not in the same location in the comment box it varies ...would substr still work ?
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.
Frequent Contributor
Posts: 80

Re: use prxparse finding date

Hi Chris you help me with this code couple of weeks ago Thanks it works now the request is to find Date formatted "".2016-06-07T00:16:11,,  is it possible to run this part of code
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=
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 13 replies
  • 988 views
  • 6 likes
  • 5 in conversation