BookmarkSubscribeRSS Feed
SASPhile
Quartz | Level 8

How to remove space before and after slash(/)?

Coming /  not coming  to be displayed as coming/not coming

14 REPLIES 14
novinosrin
Tourmaline | Level 20

is this value of a sas variable/macro variable source

 

or plain hardcoded text to display in the log?

SASPhile
Quartz | Level 8

SAS variable. It can have single or multiple spaces

SASKiwi
PROC Star

You are missing an example relating to your question.

Reeza
Super User

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


 

Astounding
PROC Star

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.

 

 

Shemp
Obsidian | Level 7

data test;
length var1 $ 20
var2 $ 20;
var1='abc / def / efg';
var2=prxchange('s/([^\s]*)\s*(\/)\s*([^\s]*)/$1$2$3/', -1, var1);
run;

SASPhile
Quartz | Level 8

thanks, whar are $1,$2,$3?

Shemp
Obsidian | Level 7

Those are capture buffers.

 

 

Ksharp
Super User
data _null_;
x='Coming /  not coming  ';
y=prxchange('s/\s+(?=\/)|(?<=\/)\s+//',-1,x);
put x= / y=;
run;
novinosrin
Tourmaline | Level 20

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;
FreelanceReinh
Jade | Level 19

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.).

novinosrin
Tourmaline | Level 20

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

FreelanceReinh
Jade | Level 19

@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.

Shemp
Obsidian | Level 7

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: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

Mastering the WHERE Clause in PROC SQL

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.

Discussion stats
  • 14 replies
  • 2548 views
  • 1 like
  • 8 in conversation