Date: Wed, 29 Oct 2008 17:13:21 -0500
Reply-To: "./ ADD NAME=Data _null_," <iebupdte@GMAIL.COM>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: "./ ADD NAME=Data _null_," <iebupdte@GMAIL.COM>
Subject: Re: how to clean characters from number data
In-Reply-To: <200810292124.m9TGcrvZ001789@malibu.cc.uga.edu>
Content-Type: text/plain; charset=ISO-8859-1
Don't forget about the new parameters. A for letters a-z and I for ignore case.
953 data _null_;
954 input cWeight $;
955 weight = compress(cWeight,,'AI');
956 put (_all_)(=);
957 cards;
cWeight=112 weight=112
cWeight=130kg weight=130
cWeight=100 weight=100
cWeight=123kilog weight=123
cWeight=any99non weight=99
On 10/29/08, Ya Huang <ya.huang@amylin.com> wrote:
> Not too hard for compress, just a bit long:-)
>
> data want;
> set have;
> y = input(compress(upcase(x),'ABCDEFGHIJKLMNOPQRSTUVWXYZ '),best.);
> run;
>
>
> On Wed, 29 Oct 2008 17:16:29 -0400, Mike Zdeb <msz03@ALBANY.EDU> wrote:
>
> >hi ... agreed, but ...
> >
> >#1 I wanted an 'any digit' solution and that's hard (possible?) with one
> COMPRESS or one TRANWRD
> >#2 I wanted a numeric X
> >
> >so, making PRXPARSE 'sparse' ...
> >
> >data have;
> >input x $15.;
> >datalines;
> >112
> >130kg
> >100 kg
> >123kilogams
> >any99non99digit
> >;
> >run;
> >
> >data want;
> >set have (rename=(x=y)) end=done;
> >x = input(prxchange("s/\D//",-1,y),best.);
> >keep x;
> >run;
> >
> >--
> >Mike Zdeb
> >U@Albany School of Public Health
> >One University Place
> >Rensselaer, New York 12144-3456
> >P/518-402-6479 F/630-604-1475
> >
> >> Mike ,
> >>
> >> It gets simplier than that with RegEx:
> >>
> >> Untest:
> >>
> >> Data Need ;
> >> Set Have ;
> >>
> >> X = PrxChange( 's/^(.*)kg$/$1/io' , 1 , X ) ;
> >>
> >> Run ;
> >>
> >>
> >> However, since compress or tranwrd could do this and they are SAS
> >> functions, Id go with those given the overhead that calling Perl would
> >> take. SAS functions work fast than RegEx, up to the point where one
> would
> >> have to call mulitple SAS functions to accomplish the same task as one
> >> call of Perl RegEx.
> >>
> >>
> >>
> >>
> >> On Wed, 29 Oct 2008 16:29:54 -0400, Mike Zdeb <msz03@ALBANY.EDU> wrote:
> >>
> >>>hi ...
> >>>from my limited knowledge of Perl regular expressions
> >>>a way to remove any non-digits
> >>>
> >>>data have;
> >>>input x : $15.;
> >>>datalines;
> >>>112
> >>>130kg
> >>>100 kg
> >>>123kilogams
> >>>any99non99digit
> >>>;
> >>>run;
> >>>
> >>>data want;
> >>>* change non-digits to blanks;
> >>>fix = prxparse("s/\D//");
> >>>do until (done);
> >>> set have (rename=(x=y)) end=done;
> >>>* create a numeric variable from the cleaned up original value;
> >>> x = input(prxchange(fix,-1,y),best.);
> >>> output;
> >>>end;
> >>>keep x;
> >>>run;
> >>>
> >>>gives ...
> >>>
> >>>Obs x
> >>> 1 112
> >>> 2 130
> >>> 3 100
> >>> 4 123
> >>> 5 9999
> >>>
> >>>
> >>>--
> >>>Mike Zdeb
> >>>U@Albany School of Public Health
> >>>One University Place
> >>>Rensselaer, New York 12144-3456
> >>>P/518-402-6479 F/630-604-1475
> >>>
> >>>> Hi SASLs,
> >>>>
> >>>> I have a large datasets. One variable is weight. Most data were input
> >> as number, but some data were input as number with 'kg'. So the data are
> >> saved
> >>>> as character data. How could I clean all data with 'kg', and convert
> >> them to numeric data?
> >>>>
> >>>> Data have,
> >>>> 112
> >>>> 130kg
> >>>> 100 kg
> >>>>
> >>>> data want,
> >>>> 112
> >>>> 130
> >>>> 100
> >>>>
> >>>> Thanks much!
> >>>>
> >>>> Jane
> >>>>
> >>>>
> >>>>
> >>>>
> >>>>
> >>
>
|