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.
Exactly, what symbols and chars do you want to remove?
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.
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;
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);
@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.
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and save with the early bird rate—just $795!
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.
Ready to level-up your skills? Choose your own adventure.