BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
buckeyefisher
Obsidian | Level 7

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;

1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

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. 

View solution in original post

4 REPLIES 4
art297
Opal | Level 21
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

 

Patrick
Opal | Level 21

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. 

ChrisNZ
Tourmaline | Level 20

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

 

buckeyefisher
Obsidian | Level 7

This also looks intersting. Thanks !!

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

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

View all other training opportunities.

Discussion stats
  • 4 replies
  • 793 views
  • 0 likes
  • 4 in conversation