DATA Step, Macro, Functions and more

questions on two string functions

Super Contributor
Posts: 312

questions on two string functions

I encounter two interesting functions today and want to find out what they do:


1) I found it here : This can remove leading 0s from a string. But what does "+//o" do in y =prxchange('s/^0+//o',-1,x)?


data val;
x='123'; output;
x='0009876'; output;

data nozero;
   set val;
   y =prxchange('s/^0+//o',-1,x);


 I wanted to remove leading dashes, and it works by replacing 0 to -, but I still do not understand it well. 


data val;
x='1-23'; output;
x='0009-876'; output;

data nozero;
   set val;
   y =prxchange('s/^-+//o',-1,x);



 2. Another function if anyalpha:

if anyalpha(string) then string='';

anyalpha(string) will return 0 if there is no alphabeta found, or first position of an alphabeta character. In the above code, does 

if anyalpha(string) 

is the same as

if anyalpha(string) >0

 ? i.e. if string contains any alphabeta characters, then set it to missing? 

Super User
Posts: 10,538

Re: questions on two string functions

For details on search and replacement with the PRX functions do a search on " PERL Regular expressions". This is a tool ported into SAS from other environments and there is lots of documentation and examples around.


The second part is how SAS treats numeric values when used as a boolean. Any missing or 0 value is considered "false", anything else is true.

 See this code for simple example:

data _null_;
   input x;
   if x then put x= +1 "x is true";
   else put x= +1 "x is false";

So when the result of any function is > 0 then it can be used as "TRUE".


Respected Advisor
Posts: 4,659

Re: questions on two string functions

's/^0+//o' reads:


s/<1>/<2>/ : substitute a substring matching the pattern <1> by pattern <2>

^ : match the string beginning

0+ : match the character '0' one or more times

o : suffix requesting that the pattern be compiled only once instead of on every function call


so the prxChange operation substitutes a succession of one or more '0's at the beginning of the string with nothing.

Super Contributor
Posts: 312

Re: questions on two string functions

Thank you very much, both of you!
Ask a Question
Discussion stats
  • 3 replies
  • 3 in conversation