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;

  

SAS Innovate 2025: Register Now

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!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1078 views
  • 1 like
  • 4 in conversation