BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Beto16
Obsidian | Level 7
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
1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

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

13 REPLIES 13
Reeza
Super User

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;

 

 

ballardw
Super User

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.

 

 

PGStats
Opal | Level 21

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
ChrisNZ
Tourmaline | Level 20

data T;

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

B=input(A,anydtdte10.);

putlog B mmddyy.;

run;

 

06/26/16

 

Beto16
Obsidian | Level 7
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
ballardw
Super User

@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?

PGStats
Opal | Level 21

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
Beto16
Obsidian | Level 7
Yes that is how all date entries look "".2016-06-07T00:16:11,, thanks for your assistance
Reeza
Super User

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

 

SUBSTR is a fairly self explanatory function.

Beto16
Obsidian | Level 7
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

ChrisNZ
Tourmaline | Level 20

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

 

Reeza
Super User

@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.
Beto16
Obsidian | Level 7
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=

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 13 replies
  • 7111 views
  • 6 likes
  • 5 in conversation