改变变种将标签命名为SAS

时间:2015-02-26 13:44:56

标签: sas

如何将变量名称(col1,col2,....)更改为标签??

我使用proc glmmod将所有分类变量的虚拟矩阵转换为其他数据集。但我无法得到变量的原始名称。

1 个答案:

答案 0 :(得分:3)

以下是来自SAS的代码!

/* Sample data set with variables containing labels */
data t1;  
   label x='this_x' 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 num=%sysfunc(attrn(&dsid,nvars));                                                                                                 
   %do i= 1 %to  #                                                                                                                    
      %let var&i=%sysfunc(varname(&dsid,&i));                                                                                             
      %let lab&i=%sysfunc(varlabel(&dsid,&i));                                                                                            
      %if &&lab&i = %then %let lab&i=&&var&i;                                                                                            
    %end;                                                                                                                                 
   %let rc=%sysfunc(close(&dsid));                                                                                                        

   proc datasets;                                                                                                                          
     modify &dsn;                                                                                                                           
          rename                                                                                                                                 
      %do j = 1 %to  #                                                                                                                  
         %if &&var&j ne &&lab&j %then   %do;                                                                                                   
          &&var&j=&&lab&j                                                                                                                    
       %end;                                                                                                                               
    %end;;                                                                                                                               
 quit;                                                                                                                                   
 run;                                                                                                                                    

%mend chge;  

%chge(t1)

proc contents;                                                                                                                          
run;