DATA Step, Macro, Functions and more

Efficiently Amend/Edit Character String Values

Accepted Solution Solved
Reply
Regular Contributor
Posts: 212
Accepted Solution

Efficiently Amend/Edit Character String Values

[ Edited ]

Please see following dataset snippet:

 

i_X
_20205_2
_21102a_5
_20103_5
_20902_2
_21405_3
_22805_3
_21506ab_3
_22304_5
_23003_4
_20903_4

The following is what I want:

 

i_X             iX
_20205_2        _20205
_21102a_5       _21102a
_20103_5        _20103
_20902_2        _20902
_21405_3        _21405
_22805_3        _22805
_21506ab_3      _21506ab
_22304_5        _22304
_23003_4        _23003
_20903_4        _20903

Here is partial code for bringing this about:

 

data want;
set have;
iX = i_X;
run;

Next iX values have to be edited, or amended.  The final two characters of each value must be deleted.  Example:  _2 in the first case.

 

Help with this greatly appreciated.

 

Nicholas Kormanik

 

 


Accepted Solutions
Solution
‎09-05-2016 05:26 AM
Regular Contributor
Posts: 212

Re: Efficiently Amend/Edit Character String Values

Excellent contributions.  Surely useful for future reference, and searches for answers by others.

 

HOWEVER, in this present case, it appears the winner is as follows:

 

data want;
set have;
iX = scan(i_X,-2,"_");
run;

Easier to understand.

 

 

View solution in original post


All Replies
Regular Contributor
Posts: 212

Re: Efficiently Amend/Edit Character String Values

Possible answer:

 

catx("_%", iX;

Maybe something like this?

 

 

Regular Contributor
Posts: 212

Re: Efficiently Amend/Edit Character String Values

Another possibility:

 

iX = scan(iX,-2);

I haven't tried the above two approaches.

 

Thinking I'd rather wait for the experts' advice.

 

 

Regular Contributor
Posts: 212

Re: Efficiently Amend/Edit Character String Values

Whoa!  And finally there's Ksharp's (Xia Keshan) answer to a similar question:

 

Perl Regular Expression is the most simple powerful flexible method for such question:

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

Good grief.  Help Mr. Wizzard.....

 

 

Super User
Posts: 17,750

Re: Efficiently Amend/Edit Character String Values

Your strings are fixed length. Use substr. 

Regular Contributor
Posts: 212

Re: Efficiently Amend/Edit Character String Values

Reeza, unfortunately some of the strings are longer than others.  I'll change my example to show that.

 

If not fixed length, what's the best answer?

 

 

Trusted Advisor
Posts: 1,356

Re: Efficiently Amend/Edit Character String Values

1) assuming you need truncate exactly last two caharcters:

       ix = substr(i_x,1,length(ix)-2);

 

2) assuming ix is made of 2 substrings separated by underscores and

     the 1st character should be an underscore:

      ix = '_' || scan(i_x,1,'_');

Regular Contributor
Posts: 212

Re: Efficiently Amend/Edit Character String Values

Wow.  So five or six ways of doing it, so far.

 

And others are likely to follow?

 

Super User
Posts: 9,662

Re: Efficiently Amend/Edit Character String Values



data have;
input i_X $20.;
string=prxchange('s/_\d+$//',-1,strip(i_x));
cards;
_20205_2
_21102a_5
_20103_5
_20902_2
_21405_3
_22805_3
_21506ab_3
_22304_5
_23003_4
_20903_4
;
run;
Solution
‎09-05-2016 05:26 AM
Regular Contributor
Posts: 212

Re: Efficiently Amend/Edit Character String Values

Excellent contributions.  Surely useful for future reference, and searches for answers by others.

 

HOWEVER, in this present case, it appears the winner is as follows:

 

data want;
set have;
iX = scan(i_X,-2,"_");
run;

Easier to understand.

 

 

☑ This topic is SOLVED.

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

Discussion stats
  • 9 replies
  • 392 views
  • 3 likes
  • 4 in conversation