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

 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

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