0

我有一百左右的列,我想使用以下宏在 SAS 中重命名:

%macro rename1(oldvarlist, newvarlist);
  %let k=1;
  %let old = %scan(&oldvarlist, &k);
  %let new = %scan(&newvarlist, &k);
     %do %while(("&old" NE "") & ("&new" NE ""));
      rename &old = &new;
      %let k = %eval(&k + 1);
      %let old = %scan(&oldvarlist, &k);
      %let new = %scan(&newvarlist, &k);
  %end;
%mend;

这些列当前命名为 C5、C7、C9、...、C205,我想将它们重命名为 AR_0、AR_1、...、AR100。

使用上面的宏,我怎样才能将这些新名称放在以下代码的逗号之后,而不用逐个编写?

%rename1(C5--C205, # new names here #);
4

3 回答 3

1

这是一个较长的解决方案,但它是相当动态的,你很容易看到事情是如何工作的。我假设您将在 proc 数据集中使用 rename 语句。否则,您可能只是懒惰并使用数组替换然后删除旧变量,尽管这效率不高。

proc sql;
    create table oldvar as
    select name, varnum
    from sashelp.vcolumn
    where upcase(libname)='SASHELP' 
        and upcase(memname)='CLASS'
    order by varnum;
quit;

data rename;
    set oldvar;
    new_var=catx("_", "AR",varnum);
run;

proc sql noprint;
    select catx("=", name, new_var) into :rename_list 
                    separated by " "
    from rename;
quit;

%put rename &rename_list;

proc datasets library=work;
modify my_dataset;
rename &rename_list;
run;quit;
于 2014-11-17T22:07:41.630 回答
1

这将首先找到旧列并将它们重命名为 AR_# 并创建您可以使用的宏变量 varlist:

proc sql noprint;
    create table newvar as
    select name
    from sashelp.vcolumn
    where libname="SASHELP" and memname="CLASS"
    order by name;
quit;
data newvar;
  set newvar;
  name=compress("AR_"!!put(_n_,4.));
run;
proc sql noprint;
    select name into :varlist separated by " " 
    from newvar;
quit;
于 2014-11-18T10:36:37.037 回答
0

可能,这样的事情会做的工作

%macro rename2(oldvarlist, newPrefix);
%let k=1;
%let old = %scan(&oldvarlist, &k);
  %do %while(("&old" NE ""));
    rename &old = &newPrefix.&k.;
    %let k = %eval(&k + 1);
    %let old = %scan(&oldvarlist, &k);
  %end;
%mend;
于 2014-11-17T17:40:55.973 回答