LISTSERV at the University of Georgia
Menubar Imagemap
Home Browse Manage Request Manuals Register
Previous messageNext messagePrevious in topicNext in topicPrevious by same authorNext by same authorPrevious page (August 2010, week 5)Back to main SAS-L pageJoin or leave SAS-L (or change settings)ReplyPost a new messageSearchProportional fontNon-proportional font
Date:         Sun, 29 Aug 2010 23:13:02 -0400
Reply-To:     Akshaya <akshaya.nathilvar@GMAIL.COM>
Sender:       "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From:         Akshaya <akshaya.nathilvar@GMAIL.COM>
Subject:      Re: Find MAX length of values of all character variables.
Comments: To: "Data _null_," <iebupdte@gmail.com>
In-Reply-To:  <AANLkTikFKa-zvNLSsKxonf-tOtezH3Fh9hJ_+R=dGhZO@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1

Query the dictionary (Columns) table, create a macro variable and use it in the array:

Proc sql; select count(*) into:cnt from dictionary.columns where libname='SASHELP' and memname='SHOES' and type='char'; Quit;

%let cnt=&cnt;

array mlen{*} m1-m&cnt; array len{*} l1-l&cnt;

AkshayA!

On Fri, Aug 27, 2010 at 3:21 PM, Data _null_; <iebupdte@gmail.com> wrote:

> Your approach is similar to mine, measure and keep the max length for > each variable.. I was looking for a way to avoid defining an array > that I don't know the dimension of. That's why I thought an > associative array would be useful. > > On 8/27/10, Akshaya <akshaya.nathilvar@gmail.com> wrote: > > This is a sample code which we usually use for your defined problem: > > > > > > Data shoes(keep=name length); > > set sashelp.shoes(keep=_character_) end=eof; > > array c{*} _character_; array mlen{*} m1-m3; array len{*} l1-l3; > > > > do _i=1 to dim(c); > > len[_i]=length(c[_i]); > > mlen[_i]=max(mlen[_i],len[_i]); > > if eof then > > do; > > name=vname(c[_i]); length=mlen[_i]; output; > > end; > > end; > > > > retain m:; > > Run; > > > > > > > > AkshayA! > > > > > > On Thu, Aug 26, 2010 at 5:07 PM, Data _null_; <iebupdte@gmail.com> > wrote: > > > I know we've done this a million times but I can find it. My > > > searching skills need work. > > > > > > I want to measure the length of the values each character variable > > > keeping the max. My data will will have an unknown number of > > > character variables so I need some flexable code. I thought using an > > > associative array would be good. This is my prototype. It assumes > > > the input data does not use underscore variables maybe I should add a > > > few more underscores. > > > > > > > > > data _null_; > > > if 0 then set sashelp.shoes(keep=_character_); > > > length _dummy_ $1; > > > array _c[*] _character_; > > > length _i_ 8 _name_ $32 _length_ 8 _rc_ 8; > > > declare hash cv(ordered:'Y'); > > > cv.defineKey('_i_'); > > > cv.defineData('_i_','_name_','_length_'); > > > cv.defineDone(); > > > call missing(_i_,_name_,_length_); > > > if dim(_c) gt 1 then do; > > > do until(eof); > > > set sashelp.shoes(keep=_character_) end=eof; > > > do _i_ = 1 to dim(_c)-1; > > > _rc_ = cv.find(); > > > _length_ = max(_length_,length(_c[_i_])); > > > _rc_ = cv.replace(); > > > end; > > > end; > > > do _i_ = 1 to dim(_c)-1; > > > _rc_ = cv.find(); > > > _name_ = vname(_c[_i_]); > > > _length_ = max(_length_,length(_c[_i_])); > > > _rc_ = cv.replace(); > > > end; > > > end; > > > rc = cv.output(dataset:'MAXCHARLEN'); > > > stop; > > > run; > > > proc contents data=maxCharLen; > > > proc print data=maxCharLen; > > > run; > > > > > > > >


Back to: Top of message | Previous page | Main SAS-L page