Download SameLabel.sasSubmit a comment

%put WARNING: This code does not work.;
%put WARNING: modvar() is not available to Base SAS;

%macro SameLabel (source=, destination=);

  %* label variables in destination the same as in source
  %* Richard A. DeVenezia, 7/26/02

  %local this srcId dstId i nvars varname varnum varlabel rc;

  %let this = SameLabel;

  %* open tables in utility mode;

  %let srcId = %sysfunc (open (&source,V));
  %let dstId = %sysfunc (open (&destination,V));

  %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;
        %let varlabel = %qsysfunc (varlabel (&srcId, &i));
        %let rc = %sysfunc (modvar (&dstId, &varname, &varname, &varlabel));
  %else %do;
    %if &srcId = 0
      %then %put ERROR: &this: Source [&source] could not be opened.;
      %else %let srcId = %sysfunc (close (&srcId));

    %if &dstId = 0
      %then %put ERROR: &this: Destination [&destination] could not be opened.;
      %else %let dstId = %sysfunc (close (&dstId));

%mend SameLabel;

Sample code

data a;
  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;
  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

%SameLabel (source=A, destination=B)