samelabl.sas
%macro samelabl (fromData=, toData=);
%* Copy labels of source to destination
%* Richard A. DeVenezia, 12/22/99
%*;
%local this srcId dstId i nvars varname varnum varlabel labels lib mem rc;
%let this = samelabl;
%* open tables in utility mode;
%let srcId = %sysfunc (open (&fromData,V));
%let dstId = %sysfunc (open (&toData,V));
%* create name='label' part of label statement for use in Proc DATASETS;
%* only vars in both datasets will be in the statement;
%if &srcId and &dstId %then %do;
%let nvars = %sysfunc (attrn (&srcId, nvars));
%do i = 1 %to &nvars;
%let varname = %sysfunc (varname (&srcId, &i));
%let varnum = %sysfunc (varnum (&dstId, &varname));
%if &varnum %then %do;
%* qsysfunc - very important, needed to macro quote
%* labels containing literal single or double quotes ;
%let varlabel = %qsysfunc (varlabel (&srcId, &i));
%put varlabl = &varlabel;
%if %length (%superq (varlabel)) %then
%let labels = &labels &varname = %sysfunc(quote(&varlabel));
%else
%let labels = &labels &varname = ' ';
%end;
%end;
%let lib = %sysfunc (attrc (&dstId, lib));
%let mem = %sysfunc (attrc (&dstId, mem));
%let srcId = %sysfunc (close (&srcId));
%let dstId = %sysfunc (close (&dstId));
proc datasets nolist lib=&lib;
modify &mem;
label &labels;
quit;
%end;
%else %do;
%if &srcId = 0
%then %put ERROR: &this: Source [&fromData] could not be opened.;
%else %let srcId = %sysfunc (close (&srcId));
%if &dstId = 0
%then %put ERROR: &this: Destination [&toDataination] could not be opened.;
%else %let dstId = %sysfunc (close (&dstId));
%end;
%mend samelabl; Sample code
/* data a; attrib a label='Aaaaa!' length=$8 b label='Beeee!' length= 8 c label='Ceeee!' length=$4 e label='Eeeyow' length=$1 f length=$1 g label='Ee"yow' length=$1 h label="Ee'yow" length=$1 i label="E""e'yow" length=$1 ; data b; attrib a length= 8 b length= 8 c length= 8 d length= 8 label='Original' f length= 4 label='Will be blank' g h i length = $4 ; run; %samelabl (fromData=A, toData=B) proc contents data=b; run; */