I've a large data set (Data A, easily > 500K rows) with a long list of conditions (A01...A38, B02... B75, C04..... S99). I want to add variable description using an external data set (Data B), with a corresponding list of variable names and description.
I found a SAS code but it works on occasions, e.g. on A01-B20, but not when I tried to include all variables. I don't know why. Also I'm hoping the code can work efficiently as my data can be large.
The sample code is as below. The actual label data and the data to be applied (w/ 100 cases) are attached so that you can test the macro % varlabel on them, and it may help to see why the macro works on the sample data but not the actual data . Thanks. Or any other SAS codes that would work for my purpose would do too. Thanks in advance.
data labeldata; input var $ 1-3 labels $ 5-17;
datalines;
A01 Diebetes
A02 Heart disease
B01 Cancer
B02 Renal failure
;
data mydata; input ID 1 A01 3 A02 5 B01 7 B02 9;
datalines;
1 1 1 1 0
2 0 0 0 1
3 1 0 1 1
4 1 0 0 1
;
run;
%macro varlabel(indata=, lib=, labeldata=, labelvar=, labeldesc=);
data _null_;
set &labeldata;
call symput("var" || trim(left(_N_)), trim(left(&labelvar)));
call symput("label" || trim(left(_N_)), trim(left(&labeldesc)));
call symput("nobs", trim(left(_N_)));
run;
proc datasets library = &lib memtype = data nolist;
modify &indata;
label
%do i = 1 %to &nobs;
&&var&i = &&label&i
%end; ;
quit;
run;
%mend;
%varlabel(indata=mydata, lib=work, labeldata=labeldata, labelvar=var, labeldesc=labels);
proc contents data=mydata order=varnum; run;
... View more