BookmarkSubscribeRSS Feed

Just as SUBSTR can be used on the left-hand side of an equal sign, allow the same for SCAN.  For example:


if scan(varname, 2) = 'ABC' then scan(varname, 2) = 'D';

Opal | Level 21

A nice idea, but...


On the left of an assignment the length of the affected variable is already set. Would "ONE ABC TWO" be replaced by "ONE D TWO" or "ONE D  TWO" ? What if the replacement string is longer than the original?


Wouldn't a function be just as useful


varname = REPLACEW(varname, 2, "D", <characters>, <modifiers>);

Tourmaline | Level 20

Thank you SAS geniuses for helping folks like me learn more, yep the length is perhaps the factor that makes the case 


data _null_;
varname="ONE ABC TWO";
if  scan(varname, 2) = 'ABC'  then call scan(varname, 2, position, length);
put varname=;

@PGStats Sir, your reasoning makes me learn more and more. Merci beacoup! 


A few discussion items ...


You could generate either "ONE D TWO" or "ONE D   TWO" depending on what you specify to the right of the equal sign.


scan(varname, 2) = 'D' would generate "ONE D TWO" while scan(varname, 2) ='D  ' would generate "ONE D   TWO"


As with all programming, it is up to the programmer to make sure that the variable has a long enough length.  Just as it would be if you were to code:


lastname = 'Mr.' || lastname;


REPLACEW?  I like it!

Jade | Level 19

@novinosrin great example of call scan. 




Super User

Perhaps this could be an enhancement to TRANWRD with an additional option to indicate replace the "nth occurrence only" instead of the default replace all occurences. Maybe a selection of options similar to Findw for things like start nth occurrence to end nth occurrence, work from right to left instead of left to right, whole words only, case sensitive/insensitive.




Tourmaline | Level 20

@data_null__  Thank you Sir aka guru of all. I am still learning by reading a lot of your posts. I wish you posted here more often or I should have  retained my SAS L account. Have a great afternoon

SAS Employee


  The behavior that you are looking for is available now through the PRX functions.  That said, I can definitely see the benefit of having a function like this available.  I'll enter the suggestion.