Date: Wed, 19 Sep 2007 12:29:40 -0400
Reply-To: Ken Borowiak <EvilPettingZoo97@AOL.COM>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: Ken Borowiak <EvilPettingZoo97@AOL.COM>
Subject: Re: OT: ruby solution to 'Re: How do I sort tokens in a variable'
task
Roy & All,
Since I know next to nothing about Ruby, I plan on checking out this
presentation at NESUG 20 later this fall:
Adding Ruby to Your SAS Tool-Box by Daniel Olguin
Ken
On Wed, 19 Sep 2007 08:38:38 -0700, Pardee, Roy <pardee.r@GHC.ORG> wrote:
>Can't resist sharing this ruby solution, which uses approach 1.
>
> input = File.open("c:/temp/original_data.txt", "r")
> output = File.open("c:/temp/fixed_data.txt" , "w")
>
> input.each_line do |this_line|
> output.puts(this_line.chomp.split("+").sort.join("+"))
> end
>
> input.close
> output.close
>
>-Roy
>
>-----Original Message-----
>From: SAS(r) Discussion [mailto:SAS-L@LISTSERV.UGA.EDU] On Behalf Of
>Eric Hoogenboom
>Sent: Tuesday, September 18, 2007 5:23 AM
>To: SAS-L@LISTSERV.UGA.EDU
>Subject: Re: How do I sort tokens in a variable
>
>Kevin,
>
>Two possible solutions:
>
>1. Split up your token in variables, put these variables into an array
>and sort the array. Then glue the sorted vars. For a quicksort
>implementation see Paul Dorfman's
>www2.sas.com/proceedings/sugi26/p096-26.pdf
>
>2. Split up the token in observations. Sort the dataset and then put it
>back together. See code below.
>
>data token;
> input token $ 1-20;
> datalines;
>B+C+A
>D+A
>B
>run;
>
>data token2 (keep=id tok);
> set token;
> length tok $ 1;
>
> id = _N_; * IDENTIFY THE SPLITTED TOKEN;
>
> do i=1 to (length(token)+1)/2;
> tok = substr(token, 2*i-1, 1);
> output;
> end;
>run;
>
>proc sort data=token2;
>by id tok;
>run;
>
>data token3 (keep=token);
> set token2;
> by id tok;
>
> length token $ 20;
>
> retain token;
>
> if first.id then token = tok;
> else token = catx("+", token, tok);
>
> if last.id then output;
>run;
>
>
>Hth,
>Eric
|