BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Ditza1
Obsidian | Level 7

Hello,

 

I want to change every character that not ST or LI to " "

I wrote this code:

 

data _null_;
a='ELISG_ST#AT_#S_STD#D="Y"';
b=prxchange('s/[^(ST|LI)]/ /',-1,a);
put a=;
put b=;
run;

 

 

a=ELISG_ST#AT_#S_STD#D="Y"

this is the output:
b=LIS ST T S ST

this is the desire output:

LI ST ST

 

how can I use not without this [] brackets that create class group?

1 ACCEPTED SOLUTION

Accepted Solutions
ChrisNZ
Tourmaline | Level 20

^ can only be used in a [] class.

But this works too if you want another way:


data _null_;
  a='ELISG_ST#AT_#S_STD#D="Y"';
  b=prxchange('s/.*?((ST)|(LI))?.*?/$1/',-1,a);
  put a= / b=;
run;

b=LISTST

 

View solution in original post

6 REPLIES 6
Ksharp
Super User

data _null_;
a='ELISG_ST#AT_#S_STD#D="Y"';
length b $ 100;
do i=1 to length(a)-1;
 temp=substr(a,i,2);
 if temp  in ('ST' 'LI') then b=catx(' ',b,temp);
end;
put a=;
put b=;
run;
Ditza1
Obsidian | Level 7
Hi,
Thanks for the answer, but I am looking for a way to do it with regular
expressions
Ksharp
Super User
Opps.
Delete the OUTPUT statement.


Ksharp
Super User
OK. No problem.


data _null_;
a='ELISG_ST#AT_#S_STD#D="Y"';


length b $ 200;
start=1;
end=length(a);
pid=prxparse('/ST|LI/');
call prxnext(pid,start,end,a,p,l);
do while(p>0);
 b=catx(' ',b,substr(a,p,l));
 output;
 call prxnext(pid,start,end,a,p,l);
end;

put a=;
put b=;
run;


Ditza1
Obsidian | Level 7

Thanks for your help

 

I am doing this as part of learn regular expression, so I am not looking for a solution to this specific problem.

I try to understand how to do it only with regular expression.

 

if I want to change ST and LI

I will use the following code:

data _null_;
a='ELISG_ST#AT_#S_STD#D="Y"';
b=prxchange('s/(ST|LI)/ /',-1,a);
put a=;
put b=;
run;

I am looking for a way to put not on (ST|LI) 

any Ideas?

ChrisNZ
Tourmaline | Level 20

^ can only be used in a [] class.

But this works too if you want another way:


data _null_;
  a='ELISG_ST#AT_#S_STD#D="Y"';
  b=prxchange('s/.*?((ST)|(LI))?.*?/$1/',-1,a);
  put a= / b=;
run;

b=LISTST

 

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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
  • 6 replies
  • 1344 views
  • 0 likes
  • 3 in conversation