BookmarkSubscribeRSS Feed
EinarRoed
Pyrite | Level 9

I need to remove multiple undesirable symbols, chars, and spaces, from multiple variables.

This is the kind of code I'm using so far:

 

do unwanted = '"', ';', '|';
		VAR1 = compbl(transtrn(compress(VAR1,,'c'), strip(unwanted), trimn('')));
VAR2 = compbl(transtrn(compress(VAR2,,'c'), strip(unwanted), trimn('')));
VAR3 = compbl(transtrn(compress(VAR3,,'c'), strip(unwanted), trimn('')));
VAR4 = compbl(transtrn(compress(VAR4,,'c'), strip(unwanted), trimn('')));
VAR5 = compbl(transtrn(compress(VAR5,,'c'), strip(unwanted), trimn('')));
VAR6 = compbl(transtrn(compress(VAR6,,'c'), strip(unwanted), trimn('')));
VAR7 = compbl(transtrn(compress(VAR7,,'c'), strip(unwanted), trimn('')));
end;

As you can see, the same extact expression is used for all 7 variables.

Can you please suggest a more tidy way to set this up, so that I only need to use the expression 1 place?

EDIT: Don't know why is the code isn't displayed properly, here's a screenshot.

5 REPLIES 5
PeterClemmensen
Tourmaline | Level 20

Exactly, what symbols and chars do you want to remove?

EinarRoed
Pyrite | Level 9

Need to remove the pipe-symbol (|), semicolons, double quotation marks, carriage returns, and double spaces. Eventually more, I'm sure. The code seems to work well though. I'm just curious about how I can set it up so that I can reuse the same expressions for multiple variables instead of listing it (compbl etc.) on each row.

PeterClemmensen
Tourmaline | Level 20

Use an array and do something like this

 

data have;
    array v $30 var1-var10;
    do over v;
        v = 'abc|} [de f{€")(/    "ab||c';
    end;
run;

data want;
   set have;
   array v var1-var10;
   do over v;
      v = compbl(prxchange('s/[";\|]//', -1, v));
   end;
run;
PaigeMiller
Diamond | Level 26

DO OVER may still work, but it is unsupported and not listed in the SAS documentation. Better to use a supported syntax

 

do i=1 to dim(v);

  

--
Paige Miller
PeterClemmensen
Tourmaline | Level 20

@PaigeMiller thank you. Yes, the implicitly subscripted array and the 'do over' syntax are not documented anymore. 

 

Call me old-fashioned, but I still like to use the implicit array from time to time 🙂 Mainly because of the simplicity of the syntax.

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 809 views
  • 1 like
  • 3 in conversation