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 (May 2003, week 2)Back to main SAS-L pageJoin or leave SAS-L (or change settings)ReplyPost a new messageSearchProportional fontNon-proportional font
Date:         Fri, 9 May 2003 09:46:55 -0400
Reply-To:     Quentin McMullen <Quentin_McMullen@BROWN.EDU>
Sender:       "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From:         Quentin McMullen <Quentin_McMullen@BROWN.EDU>
Subject:      Re: Retaining variable label in a dataset
In-Reply-To:  <b9g8s2$isr9d$>
Content-Type: text/plain; charset="iso-8859-1"

Richard A. DeVenezia wrote: > "Steve Jones" <> wrote in message > > > Hi everyone, > > > > I was wonedering if there is a way to keep or > > 'transfer' the label of a particular variable > > to another one in a dataset. > > > Steve: > There are only functions for getting variable information at run-time. > (Search help for "SAS FUNCTIONS: Variable Information Functions") > There are no functions to set [what I see as] the 'settable' aspects > (label, format, informat) of a data step pdv variable at run-time. > Thus you need two steps, one to get the label and another to set the > label. > > data a; > attrib xyz label = 'Number of Goals'; > xyz = 1; > run; > > data b; > set a; > abc = log(xyz); > if _n_ = 1 then > call symput ('abcLabel', vlabel (xyz)); > run; > > proc datasets nolist lib=work; > modify b; > label abc = "&abcLabel"; > quit;

If you really wanted to do it in one step, you could do it with a "macro function". The below quick 'n dirty macro is horribly slow because it requires creation of sashelp.vcolumn in order to get the variable label. So, probably not a reasonable solution, but thought I'd send it anyway....

%macro GetVarLabel(data= /*accepts only 1-level name*/ ,var= );

%local dsid rc VarLabel;

%let dsid = %sysfunc ( open ( sashelp.vcolumn (where=(libname="WORK" and memname="%upcase(&data)" and upcase(name)="%upcase(&Var)" ) ) ) ) ; %if &dsid=0 %then %put ER%str()ROR: USER dsid=&dsid;

%let rc=%sysfunc(fetch(&dsid)); %if &rc ne 0 %then %put ER%str()ROR: USER rc=&rc;

%let VarLabel =%sysfunc(getvarc(&dsid,%sysfunc(varnum(&dsid,label))));

%let dsid = %sysfunc ( close(&dsid) ) ;

&VarLabel %mend GetVarLabel;

data a; x=1; y=2; label x="Label for X" y="Label for Y" ; run;

data b; set a; label x="%GetVarLabel(data=a,var=y)" y="%GetVarLabel(data=a,var=x)" ; run;

proc contents data=b; run;

And after I wrote this, I realized that Roland had probably included this in his bag of macros. And indeed he has. See %VarLabel at . Looks much better than what I just wrote, as it avoids sashelp.vcolumns...

Kind Regards, --Quentin

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