BookmarkSubscribeRSS Feed
AmrAd
Obsidian | Level 7

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

9 REPLIES 9
tarheel13
Rhodochrosite | Level 12

How many variables do you need to this for? If it is just that one, you could use SUBSTR.

AmrAd
Obsidian | Level 7
a lot of variables
AmrAd
Obsidian | Level 7
ant not all variables having same length
Reeza
Super User
You need to provide more details about specifically what the rules are and how they can be applied. Otherwise I can show you a solution that will work for exactly what you post but then won't work on your actual data.
AmrAd
Obsidian | Level 7
let me clarify more
i have values like (....abd.de..,..wer..r..,etc) as system out put
i need to remove the dots only in the begining and end of each record and keep the dots as it is between the words to be like below
abd.de
wer..r
AmrAd
Obsidian | Level 7
i think i found a solution as below

var1=tranwrd(variable,'.',' ');
final=tranwrd(srtip(var1),' ','.');
run;

it works 🙂

thanks 🙂
Reeza
Super User

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;
Tom
Super User Tom
Super User

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;
Ksharp
Super User
data have;
input string $20.;
want=prxchange('s/^\.+|\.+$//',-1,strip(string));
cards;
...Tes.t...
.....abd.de..
..wer..r...
..dum..test....
;;;;
run;

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 9 replies
  • 741 views
  • 4 likes
  • 5 in conversation