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='000asd1234';output;
x='123'; output;
x='0009876'; output;
run;
data nozero;
set val;
y =prxchange('s/^0+//o',-1,x);
run;
I wanted to remove leading dashes, and it works by replacing 0 to -, but I still do not understand it well.
data val;
x='-000asd1234';output;
x='1-23'; output;
x='0009-876'; output;
run;
data nozero;
set val;
y =prxchange('s/^-+//o',-1,x);
run;
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?
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"; datalines; 1 0 . -5 34.577 ; run;
So when the result of any function is > 0 then it can be used as "TRUE".
'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.
Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.
Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.
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.