Macros 重命名SAS变量以添加下划线

Macros 重命名SAS变量以添加下划线,macros,sas,proc-sql,Macros,Sas,Proc Sql,我有大约600个变量需要动态重命名。我使用procsql创建了一个包含变量名的宏变量。现在它们看起来像:aayyy、abcjjj、bbcjjj等等。我想在前2个或3个字符(取决于变量)后添加下划线,并保持其余字符不变。最后的变量看起来像aa_yyy,abc_jjj,bbc_jjj 有什么想法吗 libname anylib "E:\"; data anylib.table1; length aayyy eeeeee abcjjjjj bbcjjjjj abcdejd 8; run; data

我有大约600个变量需要动态重命名。我使用procsql创建了一个包含变量名的宏变量。现在它们看起来像:aayyy、abcjjj、bbcjjj等等。我想在前2个或3个字符(取决于变量)后添加下划线,并保持其余字符不变。最后的变量看起来像aa_yyy,abc_jjj,bbc_jjj

有什么想法吗

libname anylib "E:\";
data anylib.table1;
length aayyy eeeeee abcjjjjj bbcjjjjj abcdejd 8;
run;

data work.table2;
length aayyy abcjjjjj bbcjjjjj abcdejd 8;
run;

proc sql;
create table list as
select * from (
select libname, memname, name,
case
when 
    compress(substr(name, 3), substr(name, 3, 1)) = ''
    then catt(substr(name, 1, 2), '_', substr(name, 3))
when 
    compress(substr(name, 4), substr(name, 4, 1)) = ''
    then catt(substr(name, 1, 3), '_', substr(name, 4))
else '' end
    as newname
from dictionary.columns
where  libname in ('WORK', 'ANYLIB') and length(name) >= 5
and ( substr(name, 3, 1) = substr(name, 4, 1)
or substr(name, 4, 1) = substr(name, 5, 1)
    )
) where newname is not null
order by libname, memname
;
quit;

data _null_;
set list;
length stmt $200;
file "E:\renames.sas";
by libname memname;
if first.libname then do;
    stmt = 'proc datasets lib=' || libname || 'nolist nodetails;';
    put stmt;
end;
if first.memname then do;
    stmt = 'modify ' || memname || ';';
    put stmt;
    stmt = 'rename';
    put stmt;
end;
stmt = '    ' || name || ' = ' || newname;
put stmt;
if last.memname then do;
    stmt = ';';
    put stmt;
end;
if last.libname then do;
    stmt = 'quit;';
    put stmt;
end;
run;

%include "E:\renames.sas";
compress(substr(
)背后的思想是找到第三个或第四个字符重复的名称,直到名称的末尾-compress删除该字符并生成空字符串。
然后,我们在数据步骤中为
PROC数据集生成脚本并运行(%include)。

这里需要更多信息。您能给出当前代码的示例和一些示例数据吗?如果没有确定“\ux”是否为是插入的。无论哪种方式,我认为您都需要研究perl正则表达式(PRX)函数,因为在此阶段模式看起来非常不规则。它们可以通过%SYSFUNC()宏函数在宏代码中使用。您可以在每个单词上循环PRX模式,并根据其是否匹配进行更改。。。