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 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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