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;
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.
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
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.
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);
This also looks intersting. Thanks !!
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.