How to compress single,double quotes,comma,brackets so the text is not completely compressed
I have text that will be as below:
"abc,'abc' (abc) "abc"
so would like to make it to as
abc abc abc abc
same idea as @SuzanneDorinski. You can also use prxchange
[ ] --- anything mentioned in square bracket will be replace by space
data test;
length have $ 25;
input have 1 - 25;
want = compbl(prxchange('s/["'')(,]+/ /',-1, have));
datalines;
"abc,'abc' (abc) "abc"
(abc) "abc" 'abc' abc,
(abc)(abc)(abc)((abc))
(((abc,)))"abc"'abc'
;
run;
You can use TRANSLATE to change the characters to spaces. You can use COMPBL to change multiple spaces to one space. You can use STRIP to remove space at the end.
data test;
length have $ 25;
input have 1 - 25;
want = strip(
compbl(
translate(have,' ','"',
' ',"'",
' ','(',
' ',')',
' ',',')
)
);
length_of_want=length(want);
datalines;
"abc,'abc' (abc) "abc"
(abc) "abc" 'abc' abc,
(abc)(abc)(abc)((abc))
(((abc,)))"abc"'abc'
;
run;
proc print data=test;
run;
Great answer.
Note that the TRANSLATE function will take longer strings, so no need to make multiple TO/FROM pairs. Plus it will pad out short TO strings with spaces. Also STRIP() can be simplified to LEFT(). When the result is assigned to another variable the value will be padded on the right with spaces anyway.
want = compbl(left(translate(have,' ','(",'')')));
same idea as @SuzanneDorinski. You can also use prxchange
[ ] --- anything mentioned in square bracket will be replace by space
data test;
length have $ 25;
input have 1 - 25;
want = compbl(prxchange('s/["'')(,]+/ /',-1, have));
datalines;
"abc,'abc' (abc) "abc"
(abc) "abc" 'abc' abc,
(abc)(abc)(abc)((abc))
(((abc,)))"abc"'abc'
;
run;
I try to introduce single quote inside the [] and it leads to unbalanced quotes. how to replace a single quote along with other charcters in the square bracts?
like abc's
it does work for abc's. try out below code
data test;
length have $ 25;
input have 1 - 25;
want = compbl(prxchange('s/["'')(,]+/ /',-1, have));
datalines;
"abc,'abc' (abc) "abc"
(abc) "abc" 'abc' abc,
(abc)(abc)(abc)((abc))
(((abc,)))"abc"'abc'
abc's
;
run;
want = compress(have, , 'kas');
Use the modifiers,
K - keeps characters
a - alphabetic
s - spaces, tabs etc.
RTM for more details:
@SASPhile wrote:
How to compress single,double quotes,comma,brackets so the text is not completely compressed
I have text that will be as below:
"abc,'abc' (abc) "abc"
so would like to make it to as
abc abc abc abc
Hi @Reeza Happy new year to you and others in the thread/sas communities.
I am afraid your
want = compress(have, , 'kas');
will not quite meet the delimiter constraints in cases like "abc,'abc' (abc) "abc" for the reason there is no space in the highligted postion and so compress can't keep space if that doesn't exist. And most likely the same case for the 4th record and beyond. That's perhaps why @SuzanneDorinski 's strategy of replace and compbl makes sense. My 2 cents
Have great year ahead.
Ah ok, It's cool. I really like your crispy techniques and do take notes.
Nice and interesting question to experiment various ideas
data test;
input have $ 1 - 25;
length want $ 25;
array t(99) $;
j=countw(compress(have),,'p');
do _n_=1 to j;
t(_n_)=scan(compress(have),_n_,,'p');
end;
want=catx(' ',of t(*));
drop j t:;
datalines;
"abc,'abc' (abc) "abc"
(abc) "abc" 'abc' abc,
(abc)(abc)(abc)((abc))
(((abc,)))"abc"'abc'
;
run;
Sets of 3 letters is obviously not real, so just for experimental fun
data test;
input have $ 1 - 25;
length want $ 25;
array t(99) $3;
call pokelong(compress(have,,'ka'),addrlong(t(1)),25);
want=catx(' ',of t(*));
drop t:;
datalines;
"abc,'abc' (abc) "abc"
(abc) "abc" 'abc' abc,
(abc)(abc)(abc)((abc))
(((abc,)))"abc"'abc'
"abc,'abc' (abc) "abc"
;
run;
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.