| Date: | Tue, 27 Oct 2009 14:31:57 -0500 |
| Reply-To: | "Data _null_;" <iebupdte@GMAIL.COM> |
| Sender: | "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU> |
| From: | "Data _null_;" <iebupdte@GMAIL.COM> |
| Subject: | Re: Decode a string? |
|
| In-Reply-To: | <b7a7fa630910271216h781677f5veda1bd14c6e4e7d8@mail.gmail.com> |
| Content-Type: | text/plain; charset=ISO-8859-1 |
|---|
If you take advantage of the features of CATX and use VVALUE this can
be written slightly more concisely.
proc format lib=work;
value xx 1='ABC' 2='XYZL' 3='FSFGS';
quit;
data test;
input x $;
length newx $500;
do _n_ = 1 to length(x);
xN = input(substr(x,_n_,1),F1.);
newx = catx(',',newx,vvalue(xN));
end;
format xN xx.;
drop xN;
datalines;
123
112
12
222
1
11
;
run;
proc print;
run;
On 10/27/09, Joe Matise <snoopy369@gmail.com> wrote:
> proc format lib=work;
> value xx
> 1='ABC'
> 2='XYZL'
> 3='FSFGS'
> ;
> quit;
>
> data test;
> input x $;
> format newx $500.;
> newx = put(input(substr(x,1,1),BEST.),xx.);
> if length(x) ge 2 then do _t = 2 to length(x);
> newx=cats(newx,',',put(input(substr(x,_t,1),BEST.),xx.));
> end;
> put x= newx=;
> datalines;
> 123
> 112
> 12
> 1
> 11
> ;
> run;
>
> Probably could also have some fun with TRANWRD, or hash solution if you
> don't need to use a format.
>
> -Joe
>
> On Tue, Oct 27, 2009 at 2:04 PM, Ya Huang <ya.huang@amylin.com> wrote:
>
> > Hi there,
> >
> > Assuming I have a format, something like this.
> >
> > proc format;
> > value xx
> > 1='ABC'
> > 2='XYZL'
> > 3='FSFGS'
> > ;
> >
> > Now I have a string '213', and I want a decoded string 'XYZL,ABC,FSFGS'.
> > The string can be '123','312','223','1','21' etc., which is dynamically
> > generated. The decoded string should follow the order of the original
> > number in the string if more than one numbers are in the original string.
> >
> > Looking for some elegant solutions.
> >
> > Thanks
> >
> > Ya
> >
>
|