简体   繁体   English

SAS SQL 根据宏变量结果创建表

[英]SAS SQL create table depending on macro variable result

I am struggling with creating a new table in proc sql SAS depending on macro variable result.我正在努力根据宏变量结果在 proc sql SAS 中创建一个新表。

1) I want to check if necessary table exists. 1)我想检查是否存在必要的表。

2) If it exists then I want to create a new table with given parameters. 2)如果它存在,那么我想用给定的参数创建一个新表。

3) If it doesn't exist I want to create a new table with different parameters. 3)如果它不存在,我想创建一个具有不同参数的新表。

I think I know how to check if table exists (0 or 1 in log results):我想我知道如何检查表是否存在(日志结果中的 0 或 1):

        %let tex1 = %sysfunc(exist(Base.pk_&monthP1));   
        %put tex1 = &tex1.;

But I do not know how to implement this result into proc sql statement.但我不知道如何将此结果实现到 proc sql 语句中。

I need sth like this:我需要这样的东西:

        proc sql;
        create table test as
        select case when &text1 = 0 then select ...
        else 
        select ...
        end ;
        quit;

Thank you in advance for suggested solutions.提前感谢您提供建议的解决方案。

So if both tables have the same structure then the only part of the SQL code that needs to change is the FROM clause.因此,如果两个表具有相同的结构,则 SQL 代码中唯一需要更改的部分是 FROM 子句。 It is probably easier to conditionally set a macro variable to the name to use and replace the name of the dataset with a reference to the macro variable.有条件地将宏变量设置为要使用的名称并将数据集的名称替换为对宏变量的引用可能更容易。

select var1,varb, ....
  from &dsname.

Now the problem becomes one of just setting the macro variable.现在问题变成了仅设置宏变量之一。 You could do that with macro logic.你可以用宏观逻辑来做到这一点。 But you could also just do that with data step logic.但是您也可以使用数据步骤逻辑来做到这一点。

data _null_ ;
  if exist("Base.pk_&monthP1") then call symputx('dsname','table1');
  else call symputx('dsname','table2');
run;
proc sql;
   ... from &dsname. ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM