BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
SASPhile
Quartz | Level 8

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

 

1 ACCEPTED SOLUTION

Accepted Solutions
kiranv_
Rhodochrosite | Level 12

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;

View solution in original post

12 REPLIES 12
SuzanneDorinski
Lapis Lazuli | Level 10

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;
Tom
Super User Tom
Super User

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,' ','(",'')')));

 

kiranv_
Rhodochrosite | Level 12

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;
SASPhile
Quartz | Level 8

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

SASPhile
Quartz | Level 8
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
kiranv_
Rhodochrosite | Level 12

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;
Reeza
Super User
  want = compress(have, , 'kas');

Use the modifiers, 

K - keeps characters

a - alphabetic

s - spaces, tabs etc.

 

RTM for more details:

https://documentation.sas.com/?cdcId=pgmmvacdc&cdcVersion=9.4&docsetId=lefunctionsref&docsetTarget=n...


@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

 


 

novinosrin
Tourmaline | Level 20

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. 

 

 

Reeza
Super User
No it won't but the OP asked for a COMPRESS solution and this was the closest I could find. I suspect the original data is JSON or XML and that would be better overall, but that's just a guess.
novinosrin
Tourmaline | Level 20

Ah ok, It's cool. I really like your crispy techniques and do take notes. 

novinosrin
Tourmaline | Level 20

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;
novinosrin
Tourmaline | Level 20

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;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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
  • 12 replies
  • 4680 views
  • 11 likes
  • 6 in conversation