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 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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