Date: Thu, 28 Oct 2004 18:49:39 -0600
Reply-To: Jack Hamilton <JackHamilton@FIRSTHEALTH.COM>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: Jack Hamilton <JackHamilton@FIRSTHEALTH.COM>
Subject: Re: re-ordering characters in a varible
Content-Type: text/plain; charset=us-ascii
Or, if the position of the letter is fixed:
=====
41 data one;
42 input code $5.;
43 reordered = translate('51234', code, '12345');
44 put (_all_) (=);
45 cards;
code=2065A reordered=A2065
code=2045B reordered=B2045
code=2067A reordered=A2067
NOTE: The data set WORK.ONE has 3 observations and 2 variables.
=====
You can use this technique to reorder the characters of any
fixed-length string. It seems to be a bit faster than the substr()
solutions presented earlier, but I have not done extensive testing.
You could also use the regular expression functions in V9 - probably
less efficient.
--
JackHamilton@FirstHealth.com
Manager, Technical Development
Metrics Department, First Health
West Sacramento, California USA
>>> "Dunn, Toby" <Toby.Dunn@TEA.STATE.TX.US> 10/28/2004 6:35 AM >>>
Nevin,
AS general solution to were you don't have to worrie about what place
the character is in the value, or how many numbers verses characters
you
have try the following:
data one;
input code $6.;
cards;
2065a
2065AB
2045BA
20677A
;
run;
%let alpha = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz';
%let beta = '1234567890';
data two;
set one;
code =
compress(substr(reverse(code),1,(indexc(reverse(code),&beta)-1))
||
substr(code,1,(indexc(code,&alpha)-1)));
put code=;
run;
HTH
Toby Dunn
-----Original Message-----
From: SAS(r) Discussion [mailto:SAS-L@LISTSERV.UGA.EDU] On Behalf Of
Nevin Krishna
Sent: Wednesday, October 27, 2004 4:48 PM
To: SAS-L@LISTSERV.UGA.EDU
Subject: re-ordering characters in a varible
Hello All,
Suppose you have a variable (character) called "code" and that this
variable has the following appearance in a dataset:
code
2065A
2045B
2067A
now suppose you want to modify this variable so that the order is
changed and the letter appears first such as:
code
A2065
B2045
A2067
I have accomplished this already by creating two separate variables
(one
for the letter and one for the numbers), and then combining them using
|| in the new order i want.. However i am looking for a simple way of
doing this without having to create new variables...any idea?
Thanks, Nevin