Date: Tue, 13 Dec 2005 19:13:30 -0500
Reply-To: Shawn Edney <shawnedney@GMAIL.COM>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: Shawn Edney <shawnedney@GMAIL.COM>
Subject: Re: Copy the Old Labels to New Variables Created
In-Reply-To: <1134160133.631895.175140@g14g2000cwa.googlegroups.com>
Content-Type: text/plain; charset=ISO-8859-1
Phani,
How about a data step / hash / proc datasets solution. Sample code follows.
No need to create all those macro vars.
Could also be done without the hash table using arrays and functions vlabel
and vname but I think the hash table version is cleaner.
HTH,
Shawn Edney
/* create a dataset with some character and numeric vars to switch */
/* using a modified version of Peters code */
/* I added some numeric vars and mixed up the case a little */
*
data* work.yourmem;
retain a a__d b b__d c c__d ertyui ertyui__d a1 d2 www__d '4567' AN an__d BN
bn__d cn cn__d *0*;
label a= 'a_ertyu' ertyui ='12345' d2 = 'd d d2 '
c= "%sysfunc( repeat( 0123456789abcdef, 15 ))"
an='an_num' cn='cn_num';
*
run*;
*
proc* *contents* data=work.yourmem; *run*;
/* get current labels */
*
data* var_labels;
set sashelp.vcolumn (where=(libname='WORK' and memname='YOURMEM'));
keep name label;
/* need to upcase to deal with variable pairs using different case in hash
table */
name=upcase(name);
*
run*;
*
data* _null_;
length str $ *300*;
/* this will fix our variable length issues */
if *1*=*0* then set work.var_labels;
/* declare hash of vars and labels */
if _n_=*1* then do;
declare hash v(dataset:'work.var_labels');
declare hiter i('v');
v.definekey('name');
v.definedata('name','label');
v.definedone();
end;
/* use call execute to build proc datesets statements to */
/* update labels, assume something will change */
call execute ('proc datasets library=work;');
call execute ('modify yourmem;');
call execute ('label ');
/* step through the vars and make label statements */
rc = i.first();
do while (rc = *0*);
if substr(left(reverse(name)),*1*,*3*)='D__' /*all var names are in
uppercase*/ then
do;
/* if a value is found, the __d var and label are overwritten */
/* with the values returned from the hash */
name=substr(name,*1*,length(name)-*3*);
srch=v.find();
if srch=*0* then
do;
/* check if a source label is available */
/* modify label statement accordingly */
if label ='' then str=strip(name)||'__d="'||
substr('Decode of '||strip(name),*1*,min((length(name)+*10*),*256*))
||'" ';
else str=strip(name)||'__d="'||
substr('Decode of '||strip(label),*1*,min((length(label)+*10*),*256*))
||'" ';
call execute(str);
end;
end;
rc = i.next();
end;
/* end label statement with semicolon and issue quit for proc datasets */
call execute('; quit;');
stop;
*
run*;
*
proc* *contents* data=work.yourmem; *run*;
**
*Creates the following proc datasets code.*
*
proc datasets library=work;
modify yourmem;
label
BN__d="Decode of BN"
C__d="Decode of
0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcde
f0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef012345"
CN__d="Decode of cn_num"
A__d="Decode of a_ertyu"
AN__d="Decode of an_num"
ERTYUI__d="Decode of 12345"
B__d="Decode of B";
quit;
*
On 12/9/05, SAS_my_life <subramanyam.phani@gmail.com> wrote:
>
> Hello Guys,
>
> I have a Data set with List of variables like this
>
> Sub Sub__D Inform Inform__D........................ what I need to do
> is copy the labels of Sub to Sub__d with Decode infront of Label I
> tried something like this
>
> proc sql noprint;
> alter table ae_temp1
> modify SUJ__D label = 'DECODE OF patient number';
> quit;
>
> but this good only for one variable I need to look whole dataset look
> for the variables ending with __d and copy their labels.
>
> Thank you so much for the help and time I deeply appriciate all your
> help.
>
> thanks
> phani
>