Date: Fri, 4 May 2007 00:27:36 -0700
Reply-To: Rajesh <Rajesh.vishwanath@GMAIL.COM>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: Rajesh <Rajesh.vishwanath@GMAIL.COM>
Organization: http://groups.google.com
Subject: Re: Renaming variables??? but in different way
In-Reply-To: <200705032136.l43KTBei015218@mailgw.cc.uga.edu>
Content-Type: text/plain; charset="us-ascii"
HI All,
Thanks you Everyone,i was expecting one solution but now now i have
more than one.
Thanks you all for your help.
Have a great weekend everyone.
Regards,
Rajesh
Chang Chung wrote:
> On Thu, 3 May 2007 14:58:52 -0400, Richard A. DeVenezia
> <rdevenezia@WILDBLUE.NET> wrote:
>
> >Rajesh has a wide table with columns named with this construction
> >
> ><id><seq>
> >
> >id ranges over a set of alpha values such as A,B,C
> >seq ranges over a numeric set such as 1..10
> ...
>
> Hi,
>
> Well, Richard's example dataset generates 20 variables in each id: A1-A20,
> B1-B20..., but he processes only with A1--Z10, meaning that there are only
> 10 variables with "Z" prefix to start with. The code still works as
> expected, demonstrating the robustness of his code.
>
> On the other hand, mine needs one correction (sorry!):
> OLD> anymissing = 0;
> NEW> retain anymissing 0;
>
> After the correction, though I ran it against Richard's test data and
> compared the output (His code ran with all A1--Z20, too.), and got the
> identical results, except that mine is keeping the rowid variable in
> addition to others, while Richard's is not keeping it.
>
> Cheers,
> Chang
>
> %let seed=123;
>
> %macro makedata;
> data results;
> array v
> %do i=65 %to 90;
> %let id = %sysfunc(BYTE(&i));
> &id.1-&id.20
> %end;
> ;
> do rowid = 1 to 100;
> do over v;
> if ranuni(&seed.) < 0.00025 then v=.;
> else v=ceil(_i_/20)+mod(_i_,20)/100;
> end; output;
> end;
> format a1--z20 5.2;
> run;
> %mend;
>
> %macro rad(out=rad);
>
> proc means noprint data=results ;
> var a1--z20;
> output out=missing ;
> run;
>
> proc transpose data=missing(where=(_stat_='N')) out=tween;
> by _freq_;
> var a1--z20;
> run;
>
> data status;
> set tween;
>
> p = prxmatch('/\d+\s*$/',_name_);
> if p;
>
> prefix = substr(_name_,1,p-1);
> suffix = substr(_name_,p);
>
> status = 'keep';
> output;
>
> if _freq_ ne col1;
>
> status = 'drop';
> put _name_=;
> do i = 1 to nobs;
> set tween point=i nobs=nobs;
> pattern = cats('/[^\d]',suffix,'\s*$/');
> if prxmatch(pattern,_name_) then output;
> end;
>
> keep _name_ prefix suffix status;
> run;
>
> proc sql;
> create table keeps as
> select * from status
> where status='keep'
> and _name_ not in (select _name_ from status where status='drop')
> ;
> quit;
>
> data _null_;
> length keeps renames $32767;
> do until (end);
> do i = 1 by 1 until (last.prefix);
> set keeps end=end;
> by prefix;
>
> keeps = catx (' ', keeps, _name_);
>
> newname = cats(prefix,i);
> if newname ne _name_ then
> renames = catx(' ',renames,catx('=',_name_,newname));
> end;
> end;
>
> call symput ('keeps', trim(keeps));
> call symput ('renames', trim(renames));
>
> stop;
> run;
>
> %put keeps=&keeps;
> %put renames=&renames;
>
> data &out.;
> set results;
> keep &keeps;
> rename &renames;
> run;
> %mend rad;
>
> %macro cyc(out=cyc);
> %local g s vars;
>
> %let g=26;
> %let s=20;
> %let vars=a1--z20;
>
> data _null_;
>
> set results nobs=lastobs;
> array var[1:&g., 1:&s.] &vars.;
> array flag[1:&g., 1:&s.] _temporary_ (%eval(&g.*&s.)*0);
> array renTo [1:&g., 1:&s.] _temporary_ (%eval(&g.*&s.)*0);
>
> *anymissing = 0;
> retain anymissing 0;
> do i = 1 to &g.;
> do j = 1 to &s.;
> if flag[i,j]=0 then do;
> if missing(var[i,j]) then do k = 1 to &g.;
> flag[k,j] = 1;
> anymissing = 1;
> end;
> end;
> end;
> end;
>
> if _n_ = lastobs then do;
> if anymissing=0 then stop;
> call execute("data &out.;");
> call execute(" set results;");
> call execute(" drop ");
> do i = 1 to &g.;
> k = 0;
> do j = 1 to &s.;
> if flag[i,j]=1 then do;
> call execute(vname(var[i,j]));
> end; else do;
> k=k+1;
> renTo[i,j] = k;
> end;
> end;
> end;
> call execute(";");
> call execute(" rename ");
> do i = 1 to &g.;
> do j = 1 to &s.;
> if flag[i,j]=0 and j^=renTo[i,j] then do;
> r = renTo[i,j];
> call execute(
> catx("=",vname(var[i,j]),vname(var[i,r]))
> );
> end;
> end;
> end;
> call execute(";");
> call execute("run;");
> end;
> run;
> %mend cyc;
>
> /* test */
> %makedata
> %rad
> %cyc
>
> proc compare data=rad compare=cyc;
> run;
|