Date: Fri, 10 Sep 2010 07:43:26 -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: monthly update
In-Reply-To: <201009101211.o8AAs3JQ000802@willow.cc.uga.edu>
Content-Type: text/plain; charset=ISO-8859-1
Like Mr. Tabachneck I am interested in the how to solve the problem of
"aging" the variable names. I do agree that the data structure is not
good but it is interesting to me to consider how to accomplish the
task of renaming the variables.
I too tried the rename using a descending enumerated list. If you use
an ascending list there is a variable name clash M0 renamed to M1
while original M1 still exists and you get an error..
The problem with the descending enumerated list is RENAME does not
recognize the descending list properly, M10 is considered smaller
than M2 so you get an ERROR start before end. However, if the list
numbers have leading zeros then the descending rename will work.
M63-M00=M64=M01
And you could actually rename the values with leading zeros, AGE and
rename back, all in ONE rename data set option.
M0-M63 = M000-M063
M063-M000 = M064-M001
M001-M064 = M1-M64
But even that part is unnecessary as you can just rename the variables
using an ascending enumerated list with a differnt root and then
rename them back with a different start and stop, like 1-64
M0-M63 = ZZ0-ZZ63
ZZ0-ZZ63 = M1-M64
This can all take place in ONE rename data set option and with a
mechanism to determine the largest value of Mnn can be automated to
some degree.
data Trans;
do id = 1 to 3;
m0 = ranuni(1243);
output;
end;
run;
data Current;
do id = 1 to 2;
array m[*] m0-m11;
do _n_ = 1 to dim(m);
m[_n_]=_n_;
end;
output;
end;
run;
proc print;
run;
* Find max index of the M variables;
proc sql noprint;
select max(input(substr(name,2),8.)) into :m
from dictionary.columns
where libname='WORK' and memname='CURRENT' and upcase(name) eqT 'M'
;
quit;
run;
%let M=&m;
%let P=%eval(&m+1);
%put NOTE: M=&m P=&p;
data new(/*drop=M&p*/);
merge trans
current
(
rename=
(
m0-m&m = _0-_&m
_0-_&m = m1-m&p
)
);
by id;
run;
proc contents varnum;
run;
proc print;
run;
NOTE: PROC DATASETS has a similar function in the AGE statement to
RENAME data sets. This is very useful if you need to keep a number of
backups of a SAS data set.
On 9/10/10, Arthur Tabachneck <art297@netscape.net> wrote:
> Chang,
>
> While I agree with the others that a redesign would probably be the better
> solution, one could always use some variant of:
>
> data want;
> set have (rename=(m63-m10=m64-m11
> m9=m10
> m8-m0=m9-m1));
> run;
>
> Art
> ----------
> On Thu, 9 Sep 2010 10:51:42 -0400, Chang Chung <chang_y_chung@HOTMAIL.COM>
> wrote:
>
> >An interesting question posted somewhere else. It goes, in part:
> >
> >"I've got a data table with text variables named m0, m1 up to m62. Each
> month
> >this dataset is updated, with the latest month's data stored in m0 while
> all
> >the rest need to move up on index, i.e. m0 should be renamed m1, m1
> should be
> >renamed m2, etc. up to m62 which should be renamed m63."
>