2012-06-20 4 views
1

프로젝트에 늦게 들어 왔으며 SQL Server로 내보내기 위해 일부 데이터를 정규화하는 매크로를 작성하려고합니다. 표 1 (customers) 고객 고유 식별자
의 목록이있다 - - n 추가 테이블이 다음 있습니다 테이블 이름 SAS 매크로 - 여러 테이블을 하나의 테이블로 결합하여 다른 테이블로 제어

의 목록이 표 2 (hierarchy)

두 제어 테이블 ...
있다. (hierarchy)(SourceTableName 필드에 이름이 지정됨)의 각 레코드에 대해 하나씩입니다. CustomerURN, 값 1, 값

내가의 형태로, 하나의 테이블 (sample_results)로이 모든 테이블을 결합하려는 ...
- - SourceTableName, CustomerURN, 값 1 ...
의 형태로 , Value2

그러나 복사해야하는 레코드는 (customers) 테이블에있는 CustomerURN에 대한 것입니다.


나는 ... proc sql를 사용하여 하드 코딩 형식, 뭔가 같은에서

proc sql; 
insert into 
    SAMPLE_RESULTS 
select 
    'TABLE1', 
    data.* 
from 
    Table1 data 
INNER JOIN 
    customers 
    ON data.CustomerURN = customers.CustomerURN 

<repeat for every table> 

그러나 새 레코드는 hierarchy 테이블에 추가됩니다 매주이 작업을 수행 할 수 있습니다.

hierarchy 테이블에서 테이블 이름을 선택하는 루프를 작성한 다음 proc sql을 호출하여 sample_results으로 데이터를 복사 하시겠습니까?

답변

1

당신은 함께 모든 계층 구조 테이블을 연결, 단일 SQL이

proc sql ; 
    drop table all_hier_tables ; 
quit ; 

    %MACRO FLAG_APPEND(DSN) ; 
     /* Create new var with tablename */ 
     data &DSN._b ; 
     length SourceTableName $32. ; 
     SourceTableName = "&DSN" ; 
     set &DSN ; 
     run ; 

     /* Append to master */ 
     proc append data=&DSN._b base=all_hier_tables force ; 
     run ; 
    %MEND ; 

    /* Append all hierarchy tables together */ 
    data _null_ ; 
     set hierarchy ; 
     code = cats('%FLAG_APPEND(' , SourceTableName , ');') ; 
     call execute(code); /* run the macro */ 
    run ; 

    /* Now merge in... */ 
    proc sql; 
    insert into 
     SAMPLE_RESULTS 
    select 
     data.* 
    from 
     all_hier_tables data 
    INNER JOIN 
     customers 
     ON data.CustomerURN = customers.CustomerURN 
quit; 
0

또 다른 방법은 항상 메타 데이터 테이블의 최신 데이터를 반영 할 수 있도록 뷰를 생성하는 것입니다 가입 할 수 있습니다. 호출 실행 함수는 계층 구조 데이터 집합에서 테이블 이름을 읽는 데 사용됩니다. 다음은 데이터에 맞게 수정할 수 있어야하는 예제입니다. 코드의 마지막 비트가 관련성이 있습니다.

data class1 class2 class3; 
set sashelp.class; 
run; 

data hierarchy; 
input table_name $; 
cards; 
class1 
class2 
class3 
; 
run; 

data ages; 
input age; 
cards; 
11 
13 
15 
; 
run; 

data _null_; 
set hierarchy end=last; 
if _n_=1 then call execute('proc sql; create view sample_results_view as '); 
if not last then call execute('select * from '||trim(table_name)||' where age in (select age from ages) union all '); 
if last then call execute('select * from '||trim(table_name)||' where age in (select age from ages); quit;'); 
run; 
관련 문제