SAS创建多个表

SAS创建多个表,sas,Sas,我正在使用下面的代码创建7个不同的表(tmp1、tmp2…tmp7)。每个acc表都有相同的列和名称,唯一的区别是每个表都在不同的库中(test1、test2…test7)。是否可以执行某种循环,而不是在下面7次键入代码 proc sql; create table tmp1 as select * from test1.acc where datepart(timestamp) in (select max(datepart(timestam

我正在使用下面的代码创建7个不同的表(tmp1、tmp2…tmp7)。每个acc表都有相同的列和名称,唯一的区别是每个表都在不同的库中(test1、test2…test7)。是否可以执行某种循环,而不是在下面7次键入代码

proc sql;
    create table tmp1 as
    select * 
    from test1.acc 
    where datepart(timestamp) in
        (select max(datepart(timestamp)) 
              from test1.acc);
  quit;

这可以使用SAS宏语言完成,该语言专为此类情况设计。我不会进行大讨论,但下面的内容应该有用。很好的警告,我没有测试这个,所以我可能在那里的某个地方有语法错误,但如果没有其他错误,这应该给你一个完成解决方案的基础

/*宏以%macro关键字开头。宏可以接受参数,但在这种情况下不需要参数。特定于宏的命令用“%”字符表示*/

%macro read_multiple_files;
    %do i = 1 %to 7;
        /* from this point forward, the use of '&i.' will resolve to the
           appropriate loop counter 1-7 */
        proc sql;
            create table tmp&i. as
                select *
                    from test&i..acc
                        where datepart(timestamp) in
                            (select max(datepart(timestamp)) 
                                 from test&i..acc);
        run; quit;
    %end;
%mend read_multiple_files;
/* The '%mend' command doesn't really require the name of the macro to be appended, but I consider it to be good practice to include it */

/* The preceding code defines the macro program.  Now to execute the job, you NEED one last command. */

%read_multiple_files;
我希望这是有帮助的。你会发现宏编程在很多情况下都非常有用。这里有一份很好的白皮书可以开始。
.

我不清楚您的确切问题是什么,如何简化您的查询?你的时间戳有什么问题?你使用过SAS宏语言吗?这是一种避免复制和粘贴的方法。它是一种用来生成SAS代码而不是自己键入的语言。您也可以考虑将所有七个表读入一个SAS数据集。