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.

 

 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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