Sample 24720: Rename variables using the label values as the new variable names
Generate necessary code to rename variables using CALL EXECUTE.
Note:
The code produced by CALL EXECUTE runs after the current DATA step ends.
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.
/* Create sample data set with labels */
data t1;
label x='this_x' y='that_y';
do x=1,2;
do y=3,4;
output;
end;
end;
run;
/* Create macro variables to be used in the code generated by CALL EXECUTE. Although there */
/* are no character variables in the data set T1, two array statements are shown to illustrate */
/* how to group numeric and character variables (in the event there are both). Two character */
/* variables are created in the data set with a LENGTH statement therefore they are the first two */
/* character variables in the program data vector. The first DO loop begins with 3 to avoid processing */
/* those first two variables. CALL VNAME and CALL LABEL assign variable names and labels to variables */
/* and these are used to create a new variable called RENAME. When this variable is called with CALL */
/* EXECUTE, the value is returned which becomes part of the PROC DATASETS code to rename the variables. */
%let lib=work;
%let mem=t1;
data _null_;
length name label $8;
set t1;
array c(*) _character_;
array _n(*) _numeric_;
call execute('proc datasets lib=&lib; modify &mem;rename ');
do i = 3 to dim(c);
call vname(c(i),name);
call label(c(i),label);
rename=name||'='||label;
call execute( rename );
end;
do i = 1 to dim(_n);
call vname(_n(i),name);
call label(_n(i),label);
rename=name||'='||label;
call execute( rename );
end;
call execute( '; run ; quit;') ;
stop;
run;
proc contents data=t1;
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
Generate necessary code to rename variables using CALL EXECUTE.
| Type: | Sample |
| Topic: | SAS Reference ==> DATA Step SAS Reference ==> CALL routines
|
| Date Modified: | 2005-12-08 11:34:25 |
| Date Created: | 2004-09-30 14:09:07 |
Operating System and Release Information
| SAS System | Base SAS | All | n/a | n/a |