%macro genvlbls(revdate=2/19/2009 7:26:11 AM, /* This macro will generate a SAS label statement based on accessing an existing SAS data set that has variables whose values are the variable names and labels. John Blodgett, OSEDA, February, 2009 */ fileout=varlabs, /* this is a fileref which must be defined before invoking the macro. This is where the label stmt gets written. */ mod=, /* specify mod=mod to write mod to fileout */ setin=_last_, /* SAS data set to be used to get valvar and labelvar values. Ignored if values is specfied. */ setfilt=, /* a filter (where clause without the where keyword) to be applied when reading setin */ namevar= , /* variable on setin whose value is the variable name */ labelvar=, /* variable whose value will be used as the variable label. */ debug=0); %put ***** genvlbls macro, revised &revdate begin execution***** ; data _null_; set &setin end=_last; retain tic "'"; %if %superq(setfilt) ne %str() %then %do; where &setfilt ; %end; file &fileout &mod; if _n_=1 then put '*--Generated by genvlbls macro on &sysdate,--*;'/ @2 'Label '; if &namevar ne ' ' and &labelvar ne ' ' then do; *---replace single quotes with back-tics (below tilde at UL of keyboard)--; _label=translate(&labelvar,'`',tic); put @3 &namevar '=' tic $1. _label tic $1. _n+1; if _n=50 then do; put ' ;' / +1 'Label '; *---start a new statement every 50 variables----; _n=0; end; end; if _last then put ';'; run; %mend genvlbls;