BookmarkSubscribeRSS Feed
SAS_Muggle
Fluorite | Level 6

I have the following date rows:

 

VAR1

This is row 1 (row1)

This is row 2 (I think) (row2)

(row3.)

 

And I need it to look like this:

row1

row2

row3

 

Basically I need to trim the string into the text in between the last parenthesis. Can anyone help with code to perform this? 

3 REPLIES 3
PeterClemmensen
Tourmaline | Level 20

How about

 

data have;
input VAR1 $ 1-50;
datalines;
This is row 1 (row1)           
This is row 2 (I think) (row2) 
(row3.)                        
;

data want;
   if _N_=1 then r = prxparse('/\(([^)]*)\)[^(]*$/');
   set have;
   if prxmatch(r, VAR1) then var2 = prxposn(r, 1, VAR1);
   
   retain r;
   drop r;
run;

 

Result:

 

VAR1                            var2
This is row 1 (row1)            row1
This is row 2 (I think) (row2)  row2
(row3.)                         row3.
Ksharp
Super User
data have;
input VAR1 $ 1-50;
datalines;
This is row 1 (row1)           
This is row 2 (I think) (row2) 
(row3.)                        
;

data want;
 set have;  
 if substr(var1,length(var1))=')' then want=scan(var1,-1,'() ');
run;
Kurt_Bremser
Super User

Done with SCAN and COMPRESS:

data have;
input var1 $80.;
datalines;
This is row 1 (row1)
This is row 2 (I think) (row2)
(row3.)
;

data want;
set have;
var2 = compress(scan(var1,-1,"("),").");
run;

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—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
  • 1231 views
  • 1 like
  • 4 in conversation