BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
ahhh
Obsidian | Level 7

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!

 

 

1 ACCEPTED SOLUTION
9 REPLIES 9
rajeshalwayswel
Pyrite | Level 9


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

ahhh
Obsidian | Level 7
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...
rajeshalwayswel
Pyrite | Level 9


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;

Ksharp
Super User

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

ahhh
Obsidian | Level 7

Thanks everyone! it worked!

rajeshalwayswel
Pyrite | Level 9

Hi,

 

I'm wondering how it works can you please explain 

prxchange function.

 Thanks,

Rajesh

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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