0
votes

How do you take the labels contained in the metadata data set and apply them to the variables in the set1 data set?

The desired result is that 'set1' still contains variables a-h and the appropriate variables now have labels. For example 'set1' will continue to have a variable 'a' with no label, however variable 'b' will now have the label 'Label1' etc.

The code I have below works, but it's very inefficient because it runs the macro for each variable. So for every one label it has to read 'set1', apply a label and save 'set1'. When doing this with large 'set1' and 'metadata' data sets, it's quite slow.

/**********************************************************
Reads metadata - in the real case it comes from a large 
csv file
***********************************************************/
data metadata;
input var $ labels $;
datalines;
b Label1
d Label2
f Label3
;
run;

/**********************************************************
Reads 'set1' in the real case it comes from many 
even larger csv files.
***********************************************************/
data set1;
input a b c d e f g h;
datalines;
1 1 0 5 6 4 0 4
2 3 4 5 3 5 0 1
3 2 1 9 6 5 8 1
;
run;

/**********************************************************
Macro to relabel one by one
***********************************************************/
%Macro relabel(var,label);
DATA set1;
    set set1;
    label %quote(&var) = %quote(&label);
RUN;
%Mend relabel;

/**********************************************************
Steps through 'metadata' and individually calls the macro
for each obs
***********************************************************/
data _null_;
    set metadata;
    call execute('%relabel('||var||','||labels||')');
run;

proc print;
run;

/**********************************************************
Shows labels applied correctly.
***********************************************************/
proc contents;
run;
1
You will have to gen some code with metadata but you don't need to recreate SET1 just use PROC DATASETS. Basically what you have but use PROC DATASETS. - data _null_
Thanks data_null_! that definitely sped things up. - variable

1 Answers

1
votes

If your metadata is small enough then you can use a macro variable to hold it. There is a 65K limit on the size of a macro variable.

proc sql noprint;
  select catx('=',var,quote(trim(labels))) 
    into :labels separated by ' '
    from metadata
  ;
quit;

proc datasets nolist lib=work ;
  modify set1;
  label &labels;
  run;
quit;