BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Mozer19
Fluorite | Level 6

Witch function I can to use for search a date inside chars: PRXCHANGE, PRXMATCH, PRXPARSE, another one?

Data example;

char = "this_is_a_example_with_date_20230331";

/* The format date is YYYYMMDD */

resolve = ? / * function;

run;

 

output:

resolve

31/03/2023

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

PRXMATCH

 

data test;
char = "this_is_a_example_with_date_20230331";
pos = prxmatch("/(19|20)\d\d[01]\d[0123]\d/o", char);
if pos > 0 then validDate = input(substr(char,pos,8),yymmdd8.);
format validDate yymmdd10.;
drop pos;
run;

proc print;

PGStats_0-1680284110440.png

 

PG

View solution in original post

4 REPLIES 4
PGStats
Opal | Level 21

PRXMATCH

 

data test;
char = "this_is_a_example_with_date_20230331";
pos = prxmatch("/(19|20)\d\d[01]\d[0123]\d/o", char);
if pos > 0 then validDate = input(substr(char,pos,8),yymmdd8.);
format validDate yymmdd10.;
drop pos;
run;

proc print;

PGStats_0-1680284110440.png

 

PG
Mozer19
Fluorite | Level 6

@PGStats , Thank you so much! Your solution solved my problem.

Mozer19
Fluorite | Level 6

Hi, @PGStats. Are you ok?
I try to learn your solution's about prxmatch, what does that means (19|20) inside the function? Exemple: pos = prxmatch("/(19|20)\d\d[01]\d[0123]\d/o", char);

If you explain me more about every single steps of function, like a, how the function transform 20230406 in 06-04-2023, I preciate this. Can you?


Would You to show me more a function to return month and year?
And another function to return only year?

PGStats
Opal | Level 21

Here is a commented version of the code:

data test;
char = "this_is_a_example_with_date_20230331";
/*
The first parameter of the prxmatch function is a 
Perl regular expression. It is interpreted as follows
Find a substring that:
starts with 19 or 20, followed by two digits, followed by 0 or 1,
followed by a digit, followed by 0, 1, 2, or 3, followed by a digit.
If found, return the position of the beginning the matching substring.
*/
pos = prxmatch("/(19|20)\d\d[01]\d[0123]\d/o", char);
/*
If a match is found, extract the substring, read it as a SAS date with 
informat YYMMDD8.
*/
if pos > 0 then validDate = input(substr(char,pos,8),yymmdd8.);
/* 
When displaying the SAS date, use the format YYMMDD10.
*/
format validDate yymmdd10.;

drop pos;
run;

Perl regular expression syntax is described here.

To display the date as year and month, use format YYMMD.

To display the date as year only, use format YEAR4.

Note that the period is part of the format name.

 

hth

 

PG

SAS INNOVATE 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

Register now!

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.

Get the $99 certification deal.jpg

 

 

Back in the Classroom!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 239 views
  • 2 likes
  • 2 in conversation