Date: Thu, 26 Jul 2001 09:21:47 -0400
Reply-To: "Diskin, Dennis" <Dennis.Diskin@PHARMA.COM>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: "Diskin, Dennis" <Dennis.Diskin@PHARMA.COM>
Subject: Re: Another Renaming variable issue
Content-Type: text/plain
Boyd,
Here's a slightly different approach from other suggestions:
Benefits are
1. No temporary datasets are created.
2. The target dataset is not rewritten, only the directory portion is
altered.
data dataset;
is = 'abc';
inta = 5;
iast = 'qxr';
imp = 34;
abc='extra';
label is = 'IS' inta = 'INTA' iast = 'IAST' imp='IMP';
run;
proc contents;
data _null_;
set dataset(keep=i:);
array nn (*) _numeric_;
array nc (*) _character_;
call execute('proc datasets lib=work;modify dataset; rename');
length vname $ 8;
do _k = 1 to dim(nn);
call vname(nn(_k),vname);
call execute(vname|| '= I' ||left(put(_k,4.)));
end;
do _j = 1 to dim(nn);
call vname(nc(_j),vname);
call execute(vname|| '= I' ||left(put(_j-1+_k,4.)));
end;
call execute(';quit;');
stop;
run;
proc contents data=work.dataset;
run;
Results in:
-----Alphabetic List of Variables and Attributes-----
# Variable Type Len Pos Label
--------------------------------------------
5 ABC Char 5 22
2 I1 Num 8 3 INTA
4 I2 Num 8 14 IMP
1 I4 Char 3 0 IS
3 I5 Char 3 11 IAST
Potential PROBLEMS:
1. If you don't have labels, how will you know which variable is which when
you finish ? The order of the variables in the dataset determines the new
names, but in my technique, numeric variables are numbered first, followed
by character.
2. If you have any variables named I followed by a number in the original
dataset, you may not be able to rename to that name depending the order.
hth,
Dennis Diskin
> -----Original Message-----
> From: Boyd Newlin [SMTP:bnewlin@INSMED.COM]
> Sent: Wednesday, July 25, 2001 4:21 PM
> To: SAS-L@LISTSERV.UGA.EDU
> Subject: Another Renaming variable issue
>
> This might be simple but I'm trying to rename variables in a dataset
> dynamically.
>
> Conditions:
>
> 1. I only want to rename the variables that begin with an 'i' or an 'e'
> and
> rename those variables to i1, i2, i3, etc. and e1, e2, e3, etc
> respectively.
> 2. I will not know the number of variables in each set.
> 3. I do not want to hard code the old variables names.
> 4. I need it to be done dynamically.
>
> For example variable names:
>
> id add ityx ityz ityo etcci ecrti eztt
>
> Rename the above to:
>
> id add i1 i2 i3 e1 e2 e3
>
> I've tried many different renaming macros from different sources and tried
> my own but I can't seem to get it. I think I need some different eyes to
> look at it.
>
> Thanks for your help.
>
> Boyd
>
>
>
> Boyd Newlin
> Senior Programmer Analyst
> Insmed Incorporated, Biometrics
> 800 East Leigh Street
> Suite 113
> Richmond, VA 23219
> 804-828-6893 x157
> fax 804-827-1150
> bnewlin@insmed.com
|