BookmarkSubscribeRSS Feed
SASPhile
Quartz | Level 8


X20l,p5,op3
cX3z,p4,op2(1)

 

Need help on extracting values that are after letter X and before first coma.

 

for :X20l,p5,op3----20l
for:cX3z,p4,op2(1)--3z

 

5 REPLIES 5
andreas_lds
Jade | Level 19
Such problems are best solved by using a regular expression. I can't post working code right now, you need the functions prxmatch and prxposn. The regular expression should be "X(.+),“
Shemp
Obsidian | Level 7

Try this:

 

data temp;
   length extracted_value $ 10;
   input char_str $ 1-20;
   extracted_value = prxchange('s/^[^X]*X(.*?)\x2C.*$/$1/',1,char_str);
   datalines;
X20l,p5,op3
cX3z,p4,op2(1)
;

PGStats
Opal | Level 21

As suggested, a regular expression will do the job:

 

data temp;
if _n_ = 1 then prxId + prxParse("/X(\w+?),/i");
length extracted_value $ 10;
input char_str $ 1-20;
if prxMatch(prxId, char_str) then
    extracted_value = prxPosn(prxId, 1, char_str);
drop prxId;
datalines;
X20l,p5,op3
cX3z,p4,op2(1)
;

proc print data=temp noobs; run;

Remove the "i" in the pattern if the letter X at the beginning cannot be lowercase.

PG
Ksharp
Super User

If it appeared only once, that would be easy.

 

data have;
input x $40.;
cards;
X20l,p5,op3
cX3z,p4,op2(1)
;
run;
data want;
 set have;
 pid=prxparse('/(?<=x).+?(?=,)/i');
 call prxsubstr(pid,x,p,l);
 want=substr(x,p,l);
run;
Ksharp
Super User

data want;
 set have;
 pid=prxparse('/(?<=x)[^,]+(?=,)/i');
 call prxsubstr(pid,x,p,l);
 want=substr(x,p,l);
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

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 5 replies
  • 1811 views
  • 2 likes
  • 5 in conversation