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

发布于 2025-01-14 11:27:55 字数 1539 浏览 2 评论 0原文

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

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

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

indextable_namecolumn_1_name
1table_one_acolumn1
2table_one_bcolumnOne
3table_one_ccolumn_1
4table_one_dcolumn_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:

indextable_namecolumn_1_name
1table_one_acolumn1
2table_one_bcolumnOne
3table_one_ccolumn_1
4table_one_dcolumn_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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白日梦 2025-01-21 11:27:55

为什么不直接从列表中提取所有表名称,而不是使用循环呢?另外,为什么不使用单个 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

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 the APPEND procedure?

*-- 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文