DATA Step, Macro, Functions and more

Remove the last character of my string

Accepted Solution Solved
Reply
New Contributor
Posts: 4
Accepted Solution

Remove the last character of my string

Hi Guys,

I have a SAS file and one of its columns has values like this:

column : PROD_IND

value:    

ABC+

aaaaa+
sdfasf+sswgerg+sgswgrr+sgwrg+

fawef+sg+3erq+sg+dfb+dfb+

sffgggggghhhhh+

ergerg+

dfw+rg+gergh+eth+rth+hrth+

.

.

.

I just want to delete the last '+' from these values. All the values have got a '+' as the last character. I want to delete that keeping the earlier ones.

I have tried substr and compress and index... not having luck.

Please help...


Accepted Solutions
Solution
‎07-23-2012 07:26 AM
Respected Advisor
Posts: 4,173

Re: Remove the last character of my string

Posted in reply to tapas_16880

Suggest you mark answered questions as answered giving the "points" to the ones deserving them and then ask consecutive questions in a new thread referencing to the old one.

But to answer your follow up question:

I assume you have leading blanks in front of your ABC string. A "like" will find any occurence of ABC in a string, eg. xxxxABCyyyy. If you just want to find ABC but don't care about leading blanks then use where strip(PROD_IND) = 'ABC'

View solution in original post


All Replies
Super User
Posts: 19,770

Re: Remove the last character of my string

Posted in reply to tapas_16880

substr(prod_ind, 1, length(prod_ind)-1);

Valued Guide
Posts: 765

Re: Remove the last character of my string

Posted in reply to tapas_16880

hi ... lots of ways to do this ...

prod_ind = putc(prod_ind,catt('$',length(prod_ind)-1));

substr(prod_ind,length(prod_ind)) = ' ';

New Contributor
Posts: 4

Re: Remove the last character of my string

Thanks for the help... Need another help similar to this...

Same column that has now:

ABC

aaaaa+ dfg
sdfasf+sswgerg+sgswgrr+sgwrg

fawef+sg+3erq+sg+dfb+dfb

sffgggggghhhhh

ABC

xyz+qwe

ergerg

dfw+rg+gergh+eth+rth+hrth

because I have removed the last '+' symbol from this column.

Now, I need to find whenever the value is ABC and no '+' in it, then set a FLAG to 'Y'

I dont know why but when I am doing -

proc sql;

create table ddd as

select * from xyz where PROD_IND = 'ABC';quit;

this doesn't select any record but when I am using LIKE '%ABC%' then it is retrieving records.

Not sure why....

Do u have any other way to achieve this?

Solution
‎07-23-2012 07:26 AM
Respected Advisor
Posts: 4,173

Re: Remove the last character of my string

Posted in reply to tapas_16880

Suggest you mark answered questions as answered giving the "points" to the ones deserving them and then ask consecutive questions in a new thread referencing to the old one.

But to answer your follow up question:

I assume you have leading blanks in front of your ABC string. A "like" will find any occurence of ABC in a string, eg. xxxxABCyyyy. If you just want to find ABC but don't care about leading blanks then use where strip(PROD_IND) = 'ABC'

New Contributor
Posts: 4

Re: Remove the last character of my string

thanks all for the help and suppport!!! Appreciated...

Contributor
Posts: 37

Re: Remove the last character of my string

Posted in reply to tapas_16880

Hey Tapas,

I just wanted to share a simplest method to remove the last char of any string, this is amazing and working perfectly for me.

data test;

input ur_string$;

ur_string=scan(ur_string,-1);

cards;

ABC+

aaaaa+

sdfasf+

sswgerg+

sgswgrr+

sgwrg+

fawef+

sg+

3erq+

sg+

dfb+

dfb+

sffgggggghhhhh+

ergerg+

;

run;

proc print;

run;

OUTPUT:

The SAS System

    
1ABC
2aaaaa
3sdfasf
4sswgerg
5sgswgrr
6sgwrg
7fawef
8sg
93erq
10sg
11dfb
12dfb
13sffggggg
14ergerg

Thanks!!

Super User
Posts: 10,018

Re: Remove the last character of my string

Posted in reply to LittlesasMaster

Didn't you notice there are some '+' in the string ? Your code isn't going to work at this situation. Perl Regular Expression is the most simplest ,powerful,flexible method for such question.

data test;

input ur_string $40.;

string=prxchange('s/\++$//',-1,strip(ur_string));

cards;

ABC+

aa+aaa+

sdfasf+

sswgerg+

sgswgrr+

sgwrg+

fawef+

sg+

3erq+

sg+

dfb+

dfb+

sffgggggghhhhh+

ergerg+

;

run;

proc print;

run;

Xia Keshan

🔒 This topic is solved and locked.

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

Discussion stats
  • 7 replies
  • 46030 views
  • 6 likes
  • 6 in conversation