BookmarkSubscribeRSS Feed
GeorgeSAS
Lapis Lazuli | Level 10

Hello all;

 

I have a string like this:

 

"*475664/779* 475664/780* 475664/4341* 483220/1577*"

 

I want get these 4 strings locate  after "/" and before "*":

 

779, 780, 4341, 1577,

 

How to do this?

 

Another words,  If I only want keep characters  between'/' and '*' ,what should I do?

 

Thanks!

6 REPLIES 6
RW9
Diamond | Level 26 RW9
Diamond | Level 26

There are several methods, I choose this one for brevity:

data want;
  length result $200;
  string="*475664/779* 475664/780* 475664/4341* 483220/1577*";
  do i=1 to countw(string,"*");
    result=catx(",",result,scan(scan(string,i,"*"),2,'/'));
  end;
run;
Reeza
Super User

Does all of your data have this structure?

 

You can use the scan function...

 

first=scan(string, 2, "/*");
second=scan(string, 4, "/*);
PGStats
Opal | Level 21

With regular expression pattern matching:

 

data want;
if not prx0 then prx0 + prxParse("#\/[^/*]+\*#");
str = "*475664/779* 475664/780* 475664/4341* 483220/1577*";
start = 1;
call prxNext(prx0, start, -1, str, pos, len);
do subStrNo = 1 by 1 while (pos > 0);
    subStr = substr(str, pos+1, len-2);
    output;
    call prxNext(prx0, start, -1, str, pos, len);
    end;
keep subStrNo subStr;
run;

proc print data=want noobs; run;

The pattern reads: Character /, followed with one or more characters except / or *, followed by character *. 

PG
Ksharp
Super User
data _null_;
pid=prxparse("/(?<=\/)\w+(?=\*)/");
str = "*475664/779* 475664/780* 475664/4341* 483220/1577*";
start = 1;
stop=length(str);
length want $ 200;
call prxnext(pid, start, stop, str, pos, len);
do while (pos > 0);
    want=catx(',',want,substr(str, pos, len));
    call prxnext(pid, start, stop, str, pos, len);
end;
put want=;
run;
GeorgeSAS
Lapis Lazuli | Level 10

Why need two prxnext function? can we remove the first one? and how if I want to keep space symbol?

 

Thanks!

Ksharp
Super User

No. You have to have two call prxnext().

 

data _null_;
pid=prxparse("/(?<=\/)\s*\w+\s*(?=\*)/");
str = "*475664/ 779 * 475664/ 780* 475664/4341* 483220/1577*";
start = 1;
stop=length(str);
length want $ 200;
call prxnext(pid, start, stop, str, pos, len);
do while (pos > 0);
    want=catx(',',want,translate(substr(str, pos, len),'09'x,' '));
    call prxnext(pid, start, stop, str, pos, len);
end;
want=translate(want,' ','09'x);
put want=;
run;

SAS INNOVATE 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Get the $99 certification deal.jpg

 

 

Back in the Classroom!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 934 views
  • 1 like
  • 5 in conversation