Solved
Contributor
Posts: 63

# How to extract specific 4 digits from large string where the location varies

I want to extract year (4 digits) from a string which alphanumeric. The location of the year varies.

The field name is 'Preclinical' that holds the string. Typical example of the string is

"In SHR, po doses of 1-10mg/kg produced an antihypertensive effect of gradual onset and sustained (>7hr) duration due to peripheral vasodilation. It also showed suppression of the pressor response to cold stress, due to presynaptic action on the sympathetic nerve (Company communication, Fujirebio, Mar 1995). Tachycardia and cardiac depression side-effects were weaker cf nifedipine."

I want to extract 1995 from the string to precdate2. I tried the following code. But I am not any values in the output field precdate2.

prx = prxparse("/(\s)(\d\d\d\d)(\s)/");
if prxmatch(prx, Preclinical) then do;
precdate2 = prxposn(prx, 4, Preclinical);
end;

Accepted Solutions
Solution
‎05-22-2017 11:21 AM
Posts: 4,736

## Re: How to extract specific 4 digits from large string where the location varies

[ Edited ]

And to only allow the centuries 19 and 20 at the beginning of the four digit pattern:

``````data want;
set have;
prx = prxparse("/\b((19|20)\d\d)\b/");
if prxmatch(prx, Preclinical) then do;
precdate2 = prxposn(prx,1, Preclinical);
end;
run;``````

\b is used to mark a boundary so that you don't pick up 4 digits out of a string of five digits but that a 4 digit string can also be followed not only by a blank but by something like a closing bracket or a point.

All Replies
PROC Star
Posts: 8,164

## Re: How to extract specific 4 digits from large string where the location varies

```data want;
set have;
prx = prxparse("/(\d\d\d\d)/");
if prxmatch(prx, Preclinical) then do;
precdate2 = prxposn(prx,1, Preclinical);
end;
run;```

Art, CEO, AnalystFinder.com

Solution
‎05-22-2017 11:21 AM
Posts: 4,736

## Re: How to extract specific 4 digits from large string where the location varies

[ Edited ]

And to only allow the centuries 19 and 20 at the beginning of the four digit pattern:

``````data want;
set have;
prx = prxparse("/\b((19|20)\d\d)\b/");
if prxmatch(prx, Preclinical) then do;
precdate2 = prxposn(prx,1, Preclinical);
end;
run;``````

\b is used to mark a boundary so that you don't pick up 4 digits out of a string of five digits but that a 4 digit string can also be followed not only by a blank but by something like a closing bracket or a point.

PROC Star
Posts: 2,363

## Re: How to extract specific 4 digits from large string where the location varies

[ Edited ]

Or to do it differently:

``````if prxmatch("/\b(19|20)\d\d\b/o",PRECLINICAL) then
PRECDATE2 = prxchange("s/.*\b((19|20)\d\d)\b.*/\$1/o",1,PRECLINICAL);
``````

Contributor
Posts: 63

## Re: How to extract specific 4 digits from large string where the location varies

This also looks intersting. Thanks !!

☑ This topic is solved.