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!
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;
Does all of your data have this structure?
You can use the scan function...
first=scan(string, 2, "/*");
second=scan(string, 4, "/*);
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 *.
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;
Why need two prxnext function? can we remove the first one? and how if I want to keep space symbol?
Thanks!
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;
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.