BookmarkSubscribeRSS Feed
BrianB4233
Obsidian | Level 7

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"

 

BrianB4233_1-1705019338784.png

 

 

3 REPLIES 3
tarheel13
Rhodochrosite | Level 12

can you post the data in datalines? 

Patrick
Opal | Level 21

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;

Patrick_0-1705025292963.png

s_lassen
Meteorite | Level 14

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;

  

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
  • 3 replies
  • 422 views
  • 1 like
  • 4 in conversation