Agree that it would be cool to have a canned function to find the index of the Kth occurrence of a character or word.
Would be neater, as you suggest, to add an extra argument to an existing function, but if my conversations with Rick L. are any indicator, overloading existing SAS functions is much more problematic than creating new ones (I once tried to coax him into adding a parm to the CATs family to specify a format other than the default for auto-converting numeric arguments, and the above was what he said).
On the other note, the problem with the regexen is that those who don't get to use them often (or, better still, day in and day out) have to spend an inordinate time with the docs to comprehend what an expression means and does. So, simpletons like myself in cases like the subject of this thread tend to use something simpler and more readily recognizable, even at the expense of performance (as the regexen are really quite fast).
Kind regards
Paul D.
@hashman My experience is that Regular Expressions are much more expensive than the "normal" string functions.
And since they are less legible too as you mention, they are not my first choice either.
However, their power means that sometimes they are the only sensible option, as they can replace tens or even hundreds of lines of code where numerous combinations would have to be tested resulting in un-maintainable spaghetti code.
data _null_; * 0.4 seconds;
A='a, b, cd, ef, gdjh';
do I=1 to 1e7;
call scan(A,4,POS,LEN,',');
POS=POS-1;
end;
put POS=;
run;
data _null_; * 7.5 seconds;
A='a, b, cd, ef, gdjh';
do I=1 to 1e7;
POS=length(prxchange('s/(([^,]*,){3}).*/\1/o',1,A));
end;
put POS=;
run;
@ChrisNZ :
True. The reason I've mentioned the regexen's purported efficiency is that once on SAS-L someone (unfortunately, don't remember who or the circumstances) showed a regex - which I then tested - that worked much faster than the combos of string functions anyone else (including me) had offered. Could be a fluke ... but got seared in my memory nonetheless.
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!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.