Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Scanning a string for a special character

Status
Not open for further replies.

smalek

Programmer
Jan 15, 2009
28
CA
Hi
I was wondering if someone can share with me the SAS code necessary to scan a string for a specific character and then replace it.
I have a list of names of different lengths and would like to eliminate special characters from them.

For example:
AA5D16.6I
ALPR.25T
BETA.1O
CAPT12.5T
CONJ.625V1
DIGO.0625T
IV.D10W250
etc.

Desired Output:
AA5D166I
ALPR25T
BETA1O
CAPT125T
CONJ625V1
DIGO0625T
IVD10W250

Thanks
 
Try the TRANWRD function.

TRANWRD(yourfield,".","")

This would scan the "yourfield" column, find any . and replace it with a null character.

~Dave
 
Smalek,

It really depends on your definition of "special character". If it is simple to get rid of periods, then dblan's tranwrd function should do fine. However, if it extends to other punctuation then, rather than multiple tranwrd functions you can check out some of the other string functions that SAS has.

After spicing up your example with some punctuation:

Compress isn't simply for getting rid of blanks; it also has modifiers as an optional third argument.

'n' is for any alphanumeric and underscore characters.
'k' is for "keep".

For more complex patterns, the Perl inspired regular expressions are very powerful. There is a bit of a learning curve, but I think they are well worth it.

The pattern below simply says "get rid of anything that isn't a letter or a number".

HTH

Code:
data have;
   input string :$20.;
   comp = compress(string,,'kn');
   prx  = prxchange('s/[^A-Z 0-9]//i',-1,string);
cards;
AA5D16.6!I
ALPR.25@#T
BETA.$$$1O
CAP%^T&12.5T
CONJ.*()625V1
DIGO.06_+=25T
IV.D10W250
;
proc print;run;

 
I would like to first that both kdt82 and dblan for their valuable replies.
I also wanted to share with you my attempt. After flipping through some of the SAS manuals, I was able to devise the folloing code:

Name=scan(NAME,1,'/.')||scan(Name,2,'/.')||scan(Name,3,'/.');

Any thoughts?

Cheers
 
Smalek,

Although your code will work, it does depend on you knowing the maximum number of occurences of these characters. You also have the call the scan function multiple times.

I would still stick with compress, or prx

name2=compress(name,'/.');

This will do the same and should be more efficient, flexible and readable.


HTH
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top