如何自动加入一堆sas数据集

时间:2016-02-14 20:39:53

标签: sas sas-macro

我正在尝试使用我的一些SAS操作

我有一堆表,它们都具有相同的索引(id)和相同的行数

每次调用我想写的函数时,表的数量都会有所不同。我打算将表的名称存储在数据集中以循环它们。 有时只会有3个表,但有时可能会有数百个

如何为所有表编写自动连接,但只保留“ID”的一列 - 所有表中都存在ID,所以我想将它们从我加入的第一个表中删除

2 个答案:

答案 0 :(得分:0)

如果您希望PROC SQL为您找出关键变量,请使用NATURAL JOIN操作。如果有些ID会出现在某些数据集中而非其他数据集中,则请使用NATURAL FULL JOIN

data a (keep=id a1 a2) b(keep=id b1 b2) c(keep=id c1 c2) ;
  input id a1 a2 b1 b2 c1 c2 ;
  if id ne 1 then output a;
  if id ne 2 then output b;
  if id ne 3 then output c;
cards;
1 1 2 3 4 5 6
2 7 8 9 10 11 12
3 13 14 15 16 17 18 
;;;;
proc sql noprint ;
  create table want as
  select * from c natural full join 
   (select * from b natural full join a)
  ;
quit;

哪个收益率:

enter image description here

答案 1 :(得分:0)

我认为以下内容会做你正在寻找的事情。仔细看看,如果您有任何问题,请回复:

 data test.table1;                                                                                                                      
 ID = 1;                                                                                                                                
 A=7;                                                                                                                                   
 B=8;                                                                                                                                   
 OUTPUT;                                                                                                                                
 ID = 2;                                                                                                                                
 A=9;                                                                                                                                   
 B=10;                                                                                                                                  
 OUTPUT;                                                                                                                                
 ID = 3;                                                                                                                                
 A=11;                                                                                                                                  
 B=12;                                                                                                                                  
 OUTPUT;                                                                                                                                
 RUN;                                                                                                                                   


 data test.table2;                                                                                                                      
 ID = 1;                                                                                                                                
 C=9;                                                                                                                                   
 D=10;                                                                                                                                  
 OUTPUT;                                                                                                                                
 ID = 2;                                                                                                                                
 C=11;                                                                                                                                  
 D=12;                                                                                                                                  
 OUTPUT;                                                                                                                                
 ID = 3;                                                                                                                                
 C=13;                                                                                                                                  
 D=14;                                                                                                                                  
 OUTPUT;                                                                                                                                
 RUN;                                                                                                                                   


 data test.table3;                                                                                                                      
 ID = 1;                                                                                                                                
 E=11;                                                                                                                                  
 F=12;                                                                                                                                  
 OUTPUT;                                                                                                                                
 ID = 2;                                                                                                                                
 E=13;                                                                                                                                  
 F=14;                                                                                                                                  
 OUTPUT;                                                                                                                                
 ID = 3;                                                                                                                                
 E=15;                                                                                                                                  
 F=16;                                                                                                                                  
 OUTPUT;                                                                                                                                
 RUN;                                                                                                                                   


proc sql noprint;                                                                                                                       
    create table test.names_tables as                                                                                                   
    select distinct libname,memname                                                                                                     
    from sashelp.vcolumn                                                                                                                
    where libname = "TEST"                                                                                                              
    and substr(memname,1,5)="TABLE";                                                                                                    
quit;                                                                                                                                   

proc sql noprint;                                                                                                                       
    select count(*) into: cnt                                                                                                           
    from test.names_tables;                                                                                                             
quit;                                                                                                                                   

%let cnt = &cnt;                                                                                                                        

%macro jmac;                                                                                                                            


proc sql noprint;                                                                                                                       
     select libname,memname into: lib, :table                                                                                           
     from test.names_tables                                                                                                             
     where monotonic() = 1;                                                                                                             
quit;                                                                                                                                   

%let lib = &lib;                                                                                                                        
%let table = &table;                                                                                                                    

%let fname = &lib..&table;                                                                                                              

data test.tablejoin;                                                                                                                    
set &fname;                                                                                                                             
run;                                                                                                                                    

%let fname = test.tablejoin;                                                                                                            

%do i=2 %to &cnt;                                                                                                                       
proc sql noprint;                                                                                                                       
     select libname,memname into: lib1, :table1                                                                                         
     from test.names_tables                                                                                                             
     where monotonic() = &i;                                                                                                            
quit;                                                                                                                                   

%let fname1 = &lib1..&table1;                                                                                                           


proc sql noprint;                                                                                                                       
     create table &fname as                                                                                                             
     select a.*,b.*                                                                                                                     
     from &fname as a                                                                                                                   
     full join                                                                                                                          
          &fname1 as b                                                                                                                  
     on a.id = b.id;                                                                                                                    
quit;                                                                                                                                   


%end;                                                                                                                                   

%mend jmac;                                                                                                                             
%jmac;