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
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.
Possible answer:
catx("_%", iX;
Maybe something like this?
Another possibility:
iX = scan(iX,-2);
I haven't tried the above two approaches.
Thinking I'd rather wait for the experts' advice.
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.....
Your strings are fixed length. Use substr.
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?
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,'_');
Wow. So five or six ways of doing it, so far.
And others are likely to follow?
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;
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.
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.