DATA Step, Macro, Functions and more

Get rid of initial string

Reply
Frequent Contributor
Posts: 77

Get rid of initial string

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
Regular Contributor
Posts: 241

Re: Get rid of initial string

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


   */

Frequent Contributor
Posts: 77

Re: Get rid of initial string

Thanks, this is great!
Regular Contributor
Posts: 165

Re: Get rid of initial string

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]
Super User
Posts: 9,682

Re: Get rid of initial string

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
Ask a Question
Discussion stats
  • 4 replies
  • 220 views
  • 0 likes
  • 4 in conversation