DATA Step, Macro, Functions and more

Remove character is a string

Accepted Solution Solved
Reply
Contributor
Posts: 36
Accepted Solution

Remove character is a string

Hi,

 

I have a field that contains a string in many different formats. What I want to do is remove the last character in a string. Please see below:

 

Have

data one; 
input name $1-24; 
cards;
BANGE RICHARD M
AARON J BOWLER
;
run;

data two;
set one;
length new_name $24;
new_name=substr(name, 1, length(name)-1);
run; 

proc print; 
run;

 

Want

BANGE RICHARD
AARON J BOWLER

 

The code above works for the top record bit incorrecly removes a character from Bowler. Is there a simpe way to remove the last character where its not attached to another string but ignore full words?


Accepted Solutions
Solution
‎08-02-2017 06:22 AM
Super User
Super User
Posts: 7,970

Re: Remove character is a string

data two;
set one;
length new_name $24;
new_name=ifc(char(name,lengthn(name)-1)=" ",substr(name,1,lengthn(name)-1),name);
new_name=ifc(char(new_name,2)=" ",substr(new_name,3),new_name);
run;

View solution in original post


All Replies
Super User
Super User
Posts: 7,970

Re: Remove character is a string

[ Edited ]

Well an if could sort that:

data one; 
input name $1-24;
cards;
BANGE RICHARD M
AARON J BOWLER
;
run;
data two;
set one;
length new_name $24;
new_name=ifc(char(name,lengthn(name)-1)=" ",substr(name,1,lengthn(name)-1),name);
run;
New Contributor
Posts: 2

Re: Remove character is a string

You can try using a coditional statement :- 

 

data one;
input name $1-24;
cards;
BANGE RICHARD M
AARON J BOWLER
;
run;

 

data two;
set one;
length new_name $24;

if length(scan(reverse(name),1)) = 1 then
new_name = substr(name, 1, length(name)-1);
else new_name = name;
run;

 

proc print;
run;

 

do let me know for your suggestions.

Contributor
Posts: 36

Re: Remove character is a string

Posted in reply to Gautam0072

HI,

 

I like this suggestion also. How would you cleanse the name "A IAN GILES" to "IAN GILES" in the same syntax? Thanks

Contributor
Posts: 36

Re: Remove character is a string

Thanks that seems to work fine. Just going through this poor quality data. Would you know how to deal with records like the below as part of the same syntax:

 

Have

A IAN GILES

 

Want

IAN GILES

 

I should be then good to go to split the names into forenames and surnames and then create a matchkey to match data in different files. Many thanks for your help.

New Contributor
Posts: 2

Re: Remove character is a string

Hi, 

 

according to me you can again use the same set of conditional statements. with a few changes

 

data one;
input name $1-24;
cards;
BANGE RICHARD M
AARON J BOWLER
;
run;

 

data two;
set one;
length new_name $24;

if length(scan(name,1)) = 1 then
new_name = substr(name, 3);
else new_name = name;
run;

 

proc print;
run;

 

guess that would work. Smiley Happy

Solution
‎08-02-2017 06:22 AM
Super User
Super User
Posts: 7,970

Re: Remove character is a string

data two;
set one;
length new_name $24;
new_name=ifc(char(name,lengthn(name)-1)=" ",substr(name,1,lengthn(name)-1),name);
new_name=ifc(char(new_name,2)=" ",substr(new_name,3),new_name);
run;
Trusted Advisor
Posts: 1,137

Re: Remove character is a string

Alternatively you could try the perl regular expression which will achieve the expected output from a single line code

 

data one; 
input name $1-24; 
newname=prxchange('s/^\w\s|\w$/ /',-1,strip(name));
cards;
BANGE RICHARD M
AARON J BOWLER
A IAN GILES
;
run;
Thanks,
Jag
☑ This topic is solved.

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

Discussion stats
  • 7 replies
  • 137 views
  • 1 like
  • 4 in conversation