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.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

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
  • 1034 views
  • 1 like
  • 5 in conversation