How to remove a special character in beginning and last of specific variable
Example "...Tes.t..." and needs to be "Tes.t" as compress function removes all dots
How many variables do you need to this for? If it is just that one, you could use SUBSTR.
That replaces your dots in the middle with spaces which wasn't what you said you wanted to do.
data have;
input string $20.;
cards;
.....abd.de..
..wer..r...
..dum..test....
;;;;
run;
data want;
set have;
start=anyalpha(string);
end=anyalpha(left(reverse(string)));
want = substr(string, start, length(string) - end - start + 2);
var1=tranwrd(string,'.',' ');
final=tranwrd(strip(string),' ','.');
run;
You probably want to use TRANSLATE() so you can swap the periods and spaces and then swap them back. If you want to remove the trailing periods from values that last have trailing spaces then use TRIM() on the value first.
You can then apply that to a series of variables by using an ARRAY.
For example you could try it on all of the character variables.
data want ;
set have;
array _c _character_;
do over _c;
_c=translate(strip(translate(trim(_c),'. ',' .')),'. ',' .');
end;
run;
data have; input string $20.; want=prxchange('s/^\.+|\.+$//',-1,strip(string)); cards; ...Tes.t... .....abd.de.. ..wer..r... ..dum..test.... ;;;; 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!
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.