How to remove space before and after slash(/)?
Coming / not coming to be displayed as coming/not coming
is this value of a sas variable/macro variable source
or plain hardcoded text to display in the log?
SAS variable. It can have single or multiple spaces
You are missing an example relating to your question.
Will it always have one space on either side or is it variable? Have you tried TRANWRD()?
@SASPhile wrote:
How to remove space before and after slash(/)?
Coming / not coming to be displayed as coming/not coming
If you have at most one slash per line, this would do it:
if index(var, '/') then var = strip(scan(var, 1, '/')) || '/' || strip(scan(var, 2, '/'));
If you might have multiple slashes per line, I'd turn the problem over to someone who knows parsing functions.
data test;
length var1 $ 20
var2 $ 20;
var1='abc / def / efg';
var2=prxchange('s/([^\s]*)\s*(\/)\s*([^\s]*)/$1$2$3/', -1, var1);
run;
thanks, whar are $1,$2,$3?
Those are capture buffers.
data _null_; x='Coming / not coming '; y=prxchange('s/\s+(?=\/)|(?<=\/)\s+//',-1,x); put x= / y=; run;
For what its worth, linear approach using scan and catx seems easy minus efficiency(this is something I am not good at)
The catx safely strips the leading and trailing blanks
data have;
str="Coming / not coming/ will come/ must come/ come to party";
run;
data want;
set have;
length want_str $100;
str=compbl(str);
do _n_=1 to countw(str,'/');
temp=scan(str,_n_,'/');
want_str=catx('/',want_str,temp);
end;
drop temp;
run;
Or combine novinosrin's compbl idea with Reeza's TRANWRD() suggestion:
data want;
set have;
length want_str $100;
want_str=tranwrd(tranwrd(compbl(str),' /','/'),'/ ','/');
run;
Limitation: Removes blanks, but no other white-space characters (such as tabs, 'A0'x etc.).
Truly another golf and the fastest on Depaul lab machine. Maybe you should show some interest in soccer too as we loop end to end (my favorite sport). I tested for 10 million records
Surprisingly, both the sas functions approaches are much faster than prx on this machine and I don't know why
@novinosrin wrote:
Surprisingly, both the sas functions approaches are much faster than prx on this machine and I don't know why
The regex machinery is very powerful and flexible, but this comes at a price. I don't know the details of their inner workings, but the algorithms must be more complex (hence slower) than those behind functions like COMPBL or TRANWRD which are limited to simpler tasks.
Interestingly, the PRX approach didn't get any faster when I tested it with a blank in place of the \s metacharacter (using Ksharp's regex), although this should actually simplify the task.
For an increase in speed, try the following which uses lazy repetition factors (i.e. \s*?)
data test;
length var1 $ 100
var2 $ 100;
var1=' // / /ab c / d e f /g hij/ //kl m/ / ';
var2=prxchange('s/\s*?(\/)\s*?(?!\s)/$1/', -1, var1);
run;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.