Help using Base SAS procedures

How to get this substring

Reply
Regular Contributor
Posts: 241

How to get this substring

[ Edited ]

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!

Super User
Super User
Posts: 7,977

Re: How to get this substring

Posted in reply to GeorgeSAS

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;
Super User
Posts: 19,851

Re: How to get this substring

Posted in reply to GeorgeSAS

Does all of your data have this structure?

 

You can use the scan function...

 

first=scan(string, 2, "/*");
second=scan(string, 4, "/*);
Respected Advisor
Posts: 4,930

Re: How to get this substring

[ Edited ]
Posted in reply to GeorgeSAS

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
Super User
Posts: 10,041

Re: How to get this substring

Posted in reply to GeorgeSAS
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;
Regular Contributor
Posts: 241

Re: How to get this substring

[ Edited ]

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

 

Thanks!

Super User
Posts: 10,041

Re: How to get this substring

Posted in reply to GeorgeSAS

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;
Ask a Question
Discussion stats
  • 6 replies
  • 434 views
  • 1 like
  • 5 in conversation