The sample code on the Full Code tab uses macro logic to generate the DATASETS procedure code that is needed to rename variables to their label values.
These sample files and code examples are provided by SAS Institute Inc. "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and fitness for a particular purpose. Recipients acknowledge and agree that SAS Institute shall not be liable for any damages whatsoever arising out of their use of this material. In addition, SAS Institute will provide no support for the materials contained herein.
/* Sample data set with variables containing labels */
data t1;
label x='33a' y='that_y';
do x=1,2;
do y=3,4;
z=100;
output;
end;
end;
run;
/* Functions such as OPEN, ATTRN, VARNAME, and VARLABEL are used to retrieve variable names and */
/* labels. Macro variables are then created to hold the variable names and variable labels. */
/* We then loop through the number of variables (&num) in the data set passed to the macro (t1). */
/* If a variable contains a label, then the RENAME statement in PROC DATASETS is generated to */
/* contain the proper renaming. */
%macro chge(dsn);
%let dsid=%sysfunc(open(&dsn));
%let cnt=%sysfunc(attrn(&dsid,nvars));
%do i= 1 %to &cnt;
%let var&i=%sysfunc(varname(&dsid,&i));
%let lab&i=%sysfunc(varlabel(&dsid,&i));
%if &&lab&i = %then %let lab&i=&&var&i;
/* If the label begins with a number, add an underscore before the number */
%if %sysfunc(notdigit(%substr(&&lab&i,1,1))) = 0 %then %let lab&i=_&&lab&i;
%end;
%let rc=%sysfunc(close(&dsid));
proc datasets;
modify &dsn;
rename
%do j = 1 %to &cnt;
%if &&var&j ne &&lab&j %then %do;
&&var&j=&&lab&j
%end;
%end;;
quit;
run;
%mend chge;
%chge(t1)
proc contents;
run;
These sample files and code examples are provided by SAS Institute Inc. "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and fitness for a particular purpose. Recipients acknowledge and agree that SAS Institute shall not be liable for any damages whatsoever arising out of their use of this material. In addition, SAS Institute will provide no support for the materials contained herein.
Partial output from PROC CONTENTS Alphabetic List of Variables and Attributes # Variable Type Len Label 2 that_y Num 8 that_y 1 this_x Num 8 this_x
Type: | Sample |
Topic: | SAS Reference ==> DATA Step SAS Reference ==> CALL routines |
Date Modified: | 2020-05-26 16:05:01 |
Date Created: | 2004-09-30 14:09:07 |
Product Family | Product | Host | SAS Release | |
Starting | Ending | |||
SAS System | Base SAS | All | n/a | n/a |