BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
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

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
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;

  

View solution in original post

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;

  

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 644 views
  • 1 like
  • 4 in conversation