Hello all,
I've been working with free text - medical notes - that have been uploaded into a SAS dataset. Below are 2 examples that I've copied and pasted as well as a screenshot of the text within Notepad ++ that shows the Carriage Returns/Line Feeds. The keyword is 'carbapenem' and I've tried, without any luck, to extract only the line in the text that contains the keyword using some Regex code. Ideally, I'd like to pull out only the 1st line in example #1 and the 2nd line in example #2.
I'm looking for any tips or advice on how to pull out the keyword line using Regex or non-Regex code.
"Organism is a confirmed carbapenem.
If infection is present, preferred treatment
is with a fluoroquinolone. "
"COLONY TYPE 2
CARBAPENEM PRODUCER:
RECOMMEND A TREATMENT FOR COMPLICATED UTI OR SYSTEMIC INFECTION"
I assume that what you have are SAS text variables that contain carriage returns and/or linefeeds. To extract the relevant lines from that, you could try something like this (assuming your initial variable is called TEXT, and the found lines will be in the variable LINE):
data want;
set have;
prxid=prxparse('/[^\r\l]*carbapenem[^\r\l]*/i');
pos=1;
do until(0);
call prxsubstr(prxid,text,pos,len);
if pos=0 then leave;
line=substr(text,pos,len);
output;
pos+length;
end;
run;
can you post the data in datalines?
Below code should work
data want;
infile "c:\temp\source_text.txt" truncover termstr=crlf;
input line $1000.;
if find(upcase(line),'CARBAPENEM') then output;
run;
proc print data=want;
run;
I assume that what you have are SAS text variables that contain carriage returns and/or linefeeds. To extract the relevant lines from that, you could try something like this (assuming your initial variable is called TEXT, and the found lines will be in the variable LINE):
data want;
set have;
prxid=prxparse('/[^\r\l]*carbapenem[^\r\l]*/i');
pos=1;
do until(0);
call prxsubstr(prxid,text,pos,len);
if pos=0 then leave;
line=substr(text,pos,len);
output;
pos+length;
end;
run;
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.