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

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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