【问题标题】:change var. names to their labels SAS更改变量。名称到他们的标签 SAS
【发布时间】:2015-02-26 23:14:27
【问题描述】:

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

我使用 proc glmmod 将所有分类变量的虚拟矩阵放入其他数据集中。但我无法获取变量的原始名称。

【问题讨论】:

  • 我有60个变量,一个一个做起来太难了
  • 可以将此问题视为与此问题相反的问题stackoverflow.com/questions/27723334/…
  • GLMMOD 返回的是标签而不是变量名?如果是这种情况,那么为什么不在使用 GLMMOD 之前删除标签(如链接问题中所示)?还是 GLMMOD 使用通用名称和原始名称作为标签创建变量?示例代码和所需的输入/输出会很有用。

标签: sas


【解决方案1】:

这是来自 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;

【讨论】:

  • 太好了,谢谢你的帮助
猜你喜欢
  • 1970-01-01
  • 2020-08-23
  • 2020-05-05
  • 2015-02-27
  • 2021-12-05
  • 1970-01-01
  • 2018-06-10
  • 1970-01-01
  • 2019-02-19
相关资源
最近更新 更多