SAS:如何循环工作文件以附加多个数据库表
我有一大堆数据库表,需要将其附加到 SAS 上的一个合并表中。
- 该表列表定期更改。
- 此列表中的某些表没有使用相同的列名。
- 如果列名称不同,列表将指示等效名称。
该表列表从 csv 文件导入到 SAS 中,类似于以下数据:
index | table_name | column_1_name |
---|---|---|
1 | table_one_a | column1 |
2 | table_one_b | columnOne |
3 | table_one_c | column_1 |
4 | table_one_d | column_1_1 |
etc ........ | 等等..... | 等等.. ... |
我想附加此列表中的每个表,然后通过引用上面列表中的column_1_name 列来更改适用的名称。
以下代码改编自 此链接 说明了我希望 SAS 如何将上面列表中的表附加在一起。但是我不知道如何将上面的表列表及其列名转换为变量,以便它们可以在下面所示的宏中循环。
有没有一种方法可以将这个表列表转换为一个变量,然后我可以通过其索引号循环访问该变量?
非常感谢任何帮助。
libname dbname ODBC DSN=databaseName;
%let table = table_one_a; run;
%let column_one = column1; run;
%MACRO append_tables;
%If index =1 %Then %Do;
data first_table;
set dbname.&table.;
&column1. = column1;
keep column1 column2 etc;
run;
%End;
%Else %Do;
data later_table;
set dbname.&table.;
&column1. = column1;
keep column1 column2 etc;
run;
proc append
BASE = first_table
DATA = later_table;
run;
%End;
%MEND;
I have a large list of database tables that I need to append into one amalgamated table on SAS.
- This table list changes regularly.
- Some tables in this list don't have don't use the same column names.
- Where the column name is different, the list will indicate what the equivalent name is.
This table list is imported into SAS from a csv file and resembles the below data:
index | table_name | column_1_name |
---|---|---|
1 | table_one_a | column1 |
2 | table_one_b | columnOne |
3 | table_one_c | column_1 |
4 | table_one_d | column_1_1 |
etc ........ | etc..... | etc..... |
I want to append every table in this list, and then change the names where applicable by referencing the column_1_name column in the above list.
The below code was adapted from this link and is an illustration of how I want SAS to append the tables together from the above list. However I don't know how to convert the above list of tables with their column names into variables so they can be looped through in the below illustrated macro.
Is there a way that I can convert this list of tables into a variable that I can then looped through by its index number?
Any help is much appreciated.
libname dbname ODBC DSN=databaseName;
%let table = table_one_a; run;
%let column_one = column1; run;
%MACRO append_tables;
%If index =1 %Then %Do;
data first_table;
set dbname.&table.;
&column1. = column1;
keep column1 column2 etc;
run;
%End;
%Else %Do;
data later_table;
set dbname.&table.;
&column1. = column1;
keep column1 column2 etc;
run;
proc append
BASE = first_table
DATA = later_table;
run;
%End;
%MEND;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么不直接从列表中提取所有表名称,而不是使用循环呢?另外,为什么不使用单个
SET
语句而不是APPEND
过程?Instead of using a loop, why don't you just extract all the tables name from the list? Also, why are you not using a single
SET
statement instead of theAPPEND
procedure?