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

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

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
NKormanik
Barite | Level 11

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

9 REPLIES 9
NKormanik
Barite | Level 11

Possible answer:

 

catx("_%", iX;

Maybe something like this?

 

 

NKormanik
Barite | Level 11

Another possibility:

 

iX = scan(iX,-2);

I haven't tried the above two approaches.

 

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

 

 

NKormanik
Barite | Level 11

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.....

 

 

Reeza
Super User

Your strings are fixed length. Use substr. 

NKormanik
Barite | Level 11

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?

 

 

Shmuel
Garnet | Level 18

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,'_');

NKormanik
Barite | Level 11

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

 

And others are likely to follow?

 

Ksharp
Super User


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;
NKormanik
Barite | Level 11

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.

 

 

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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
  • 2815 views
  • 3 likes
  • 4 in conversation