Building models with SAS Enterprise Miner, SAS Factory Miner, SAS Visual Data Mining and Machine Learning or just with programming

removing first unwanted/non alphabet characters in a column

Accepted Solution Solved
Reply
Contributor
Posts: 37
Accepted Solution

removing first unwanted/non alphabet characters in a column

Hi

the have dataset is like the following (the name Liz has one blank space in front of it; Sam has / in front of it):

name     city     state

Liz        NY     NY

/Sam     SJ      NJ

Norm     LA     CA

Gloria    SC     MN

want to have the first non-alphabet character removed, so the table will be :

name     city     state

Liz          NY     NY

Sam       SJ      NJ

Norm     LA     CA

Gloria    SC     MN

what do you use to remove the unwanted characters/spaces?  Thanks.


Accepted Solutions
Solution
‎05-17-2012 04:39 PM
Occasional Contributor
Posts: 7

Re: removing first unwanted/non alphabet characters in a column

Yea, compress.  Though it works on the whole string, not just the first letter.

data a;

input name $;

datalines;

Liz

/Sam

     nancy

/Gloria

;

run;

data b;

set a;

newname = compress(name,'/');

run;

View solution in original post


All Replies
Super User
Posts: 17,963

Re: removing first unwanted/non alphabet characters in a column

Try the compress function with the appropriate modifiers or characters for your code.

name=compress(name, , 'kad');

http://support.sas.com/documentation/cdl/en/syntaxidx/64656/HTML/default/index.htm#/documentation/cd...

Can also look into the anypunct functions if you wanted as well.

Solution
‎05-17-2012 04:39 PM
Occasional Contributor
Posts: 7

Re: removing first unwanted/non alphabet characters in a column

Yea, compress.  Though it works on the whole string, not just the first letter.

data a;

input name $;

datalines;

Liz

/Sam

     nancy

/Gloria

;

run;

data b;

set a;

newname = compress(name,'/');

run;

Contributor
Posts: 37

Re: removing first unwanted/non alphabet characters in a column

Smiley Happy works for me!  thanks.

Occasional Contributor
Posts: 6

Re: removing first unwanted/non alphabet characters in a column

Hello,

 

You want to remove only special character. But before solution was it removes only "/" and also it compress all the "/".

please check this below solution.

 

data t;
infile cards;
input a $;
datalines;
/ag
a/g
ag/
/gh
;
run;

data t1;
set t;
if anyalpha(a) ne 1 then do;
a1=compress(a,substr(a,1,1));
end;
else a1=a;
run;

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 1863 views
  • 0 likes
  • 4 in conversation