SAS:如何在工作文件中循环以附加多个数据库表



我有一个数据库表的大列表,需要将其附加到SAS上的一个合并表中。

  • 此表列表定期更改
  • 此列表中的某些表没有使用相同的列名
  • 如果列名不同,则列表将指示等效名称

此表列表从csv文件导入SAS,类似于以下数据:

列1columnOne[/tr>
索引 table_name 列_1_name
1 table_one_a
2 table_one_b
3 table_one_c column_1
4 table_one_d column_1

与其使用循环,为什么不从列表中提取所有表名呢?此外,为什么不使用单个SET语句而不是APPEND过程?

*-- Create table list sample --*;
data csv_list;
length column_name $20.;
table_name = "table_one_a"; column_name = "Col1"; output;
table_name = "table_one_b"; column_name = "Column1"; output;
table_name = "table_one_c"; column_name = "Colonne1";output;
run;
*-- Create synthetic data for each table in table list --*
data temp.table_one_a;  
Col1 = 1;
run;
data temp.table_one_b;
Column1 = 2;
run;
data temp.table_one_c;
Colonne1 = 3;
run;
libname temp "/home/kermit/stackoverflow";
*-- Create macro tables with all table names + renaming --*
proc sql;
select cats(strip("temp."||table_name),"(rename=(",column_name,"=column1))") into :tables separated by " "
from csv_list;
quit;
*-- Append using set statement --*
data want;
set &tables.;
run;
*-- Tables macro is a concatenation of all table names in table list, separated by a space --*
%put &=tables;
TABLES=temp.table_one_a(rename=(Col1=column1)) temp.table_one_b(rename=(Column1=column1)) 
temp.table_one_c(rename=(Colonne1=column1))

*-- Result is the vertical combination of all tables + renaming in the tables macro --*
data _null_;
set want;
put id;
run;
column1
1   <-- table_one_a
2   <-- table_one_b
3   <-- table_one_c

最新更新