BookmarkSubscribeRSS Feed
MarcTC
Obsidian | Level 7
I am wondering if there is a simple statement which can do the following conversion.

If a string begins with X's, then get rid of all initial X's.

Some examples here:

XDANO => DANO
XXQRX => QRX
XMKR => MKR
XXXDMZ => DMZ
CANE => CANE
4 REPLIES 4
chang_y_chung_hotmail_com
Obsidian | Level 7
Here is one way.



   data _null_;


     length x y $8;


     do x = """A""XDANO""XXQRX""XMKR";


       y = prxchange("s|^X+||",1,x);


       put x= @10 y=;


     end;


   run;


   /* on log


   x=       y=


   x=A      y=A


   x=XDANO  y=DANO


   x=XXQRX  y=QRX


   x=XMKR   y=MKR


   */

MarcTC
Obsidian | Level 7
Thanks, this is great!
RickM
Fluorite | Level 6
Here is another solution.

Good luck!

[pre]
data one;
input names $;
datalines;
XDANO
XXQRX
XMKR
XXXDMZ
CANE
;

data two;
set one;
do while (upcase(char(names,1)) eq "X");
names=substr(names,2);

end;
run;
[/pre]
Ksharp
Super User
Hi.
I have another choice.


[pre]
data one;
input names $;
datalines;
XDANO
XXQRX
XMKR
XXXDMZ
CANE
;
run;
data one;;
set one;
if upcase(names) eq : 'X' then names=substr(names,verify(names,'xX'));
put names=;
run;

*or just more explicit and simple;
data one;;
set one;
names=substr(names,verify(names,'xX'));
put names=;
run;

[/pre]


Ksharp Message was edited by: Ksharp

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 4 replies
  • 1871 views
  • 0 likes
  • 4 in conversation