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;
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 16. Read more here about why you should contribute and what is in it for you!
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.