2013-09-25 2 views
-1

동적으로 이름을 바꾸려는 약 600 개의 변수가 있습니다. PROC SQL을 사용하여 변수 이름을 포함하는 매크로 변수를 만들었습니다. 바로 지금 그들은 다음과 같이 보입니다 : aayyy, abcjjjjj, bbcjjjjj, 등등. 첫 번째 2 또는 3 자 (변수에 따라) 뒤에 밑줄을 추가하고 나머지는 동일하게 유지하려고합니다. 따라서 최종 변수는 aa_yyy, abc_jjjjj, bbc_jjjj처럼 보일 것입니다.SAS 변수의 이름을 바꾸어 밑줄 추가

아이디어가 있으십니까? 압축은이 문자를 제거하고 빈 문자열을 생산 -

+0

여기에서 자세한 정보가 필요합니다. 현재 코드와 예제 데이터를 보여줄 수 있습니까? – Joe

+0

"_"이 삽입되는지 여부를 결정하는 패턴의 명시 적/공식적인 아이디어없이 명시 적으로 대답하기가 어려울 것입니다. 어느 쪽이든, 나는 패턴이이 단계에서 나에게 꽤 불규칙하게 보이는 perl 정규 표현식 (PRX) 함수를 살펴볼 필요가 있다고 생각한다. % SYSFUNC() 매크로 함수를 통해 매크로 코드에서 사용할 수 있습니다. 각 단어 위에 PRX 패턴을 반복하여 일치 여부에 따라 변경할 수 있습니다. – DJM

답변

3
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( 뒤에 아이디어는 ... 3 또는 4 문자가 이름의이 끝날 때까지 반복 이름을 찾는 것입니다. 그런 다음 데이터 단계에서 PROC DATASETS이라는 스크립트를 생성하고 실행합니다 (% 포함).

관련 문제