DATA Step, Macro, Functions and more

How to remove last character in the string if it is a '+'

Accepted Solution Solved
Reply
Contributor
Posts: 40
Accepted Solution

How to remove last character in the string if it is a '+'

Hi,

I have a variable 'name' like this -

 

data have;
input name $30. ;
datalines;
aaaaa+
bbb+aa+
dd+cc
fff+aa+dd+
gg+d
;
run;

 

I need the last character in the string to be removed only if it is a '+'. Some observations don't end in a '+' and those should be left as is. Also the '+' in other positions in the variable should be left as is, only if it is in the last position, it needs to be remove. How do I do this? Any advice is appreciated!

 

 

This is how I want it to look:

 

Name

aaaaa 
bbb+aa 
dd+cc 
fff+aa+dd
gg+d

 

 

Thanks!

 

 


Accepted Solutions
Solution
‎10-30-2017 11:26 AM
Super User
Posts: 9,886

Re: How to remove last character in the string if it is a '+'

Alternative:

data want;
set have;
if substr(name,length(name),1) = '+' then name = substr(name,1,length(name)-1);
run;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code

View solution in original post


All Replies
Frequent Contributor
Posts: 86

Re: How to remove last character in the string if it is a '+'


data have;
input name $30. ;
a=compress(name,' ','ka');
datalines;
aaaaa+
bbb+aa+
dd+cc
fff+aa+dd+
gg+d
;
run;

 

by using compress function with keep only alphabetics we can remove '+' from the given data

Contributor
Posts: 40

Re: How to remove last character in the string if it is a '+'

Posted in reply to rajeshalwayswel
Thanks! But, I would still like to keep the '+' in the middle. Only if the string ends with a '+' , only that '+' needs to be removed. Other '+'s should be left as is...
Frequent Contributor
Posts: 86

Re: How to remove last character in the string if it is a '+'


data have;
input name $30. ;
if substr(reverse(strip(name)),1,1)='+' then a=reverse(substr(reverse(strip(name)),2));
else a=name;
datalines;
aaaaa+
bbb+aa+
dd+cc
fff+aa+dd+
gg+d
;
run;

Solution
‎10-30-2017 11:26 AM
Super User
Posts: 9,886

Re: How to remove last character in the string if it is a '+'

Alternative:

data want;
set have;
if substr(name,length(name),1) = '+' then name = substr(name,1,length(name)-1);
run;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Super User
Posts: 10,686

Re: How to remove last character in the string if it is a '+'


data have;
input name $30. ;
want=prxchange('s/\++$//',1,strip(name));
datalines;
aaaaa+
bbb+aa+
dd+cc
fff+aa+dd+
gg+d
;
run;

Contributor
Posts: 40

Re: How to remove last character in the string if it is a '+'

Thank you!
Contributor
Posts: 40

Re: How to remove last character in the string if it is a '+'

Thank you!
Contributor
Posts: 40

Re: How to remove last character in the string if it is a '+'

Thanks everyone! it worked!

Frequent Contributor
Posts: 86

Re: How to remove last character in the string if it is a '+'

Hi,

 

I'm wondering how it works can you please explain 

prxchange function.

 Thanks,

Rajesh

☑ This topic is solved.

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

Discussion stats
  • 9 replies
  • 1583 views
  • 0 likes
  • 4 in conversation