传递SAS PROC SQL中的表值

发布于 2025-01-27 09:13:34 字数 845 浏览 2 评论 0原文

我有一个table_a。 的表_a的示例数据

带有名称和日期名称日期
标记29-apr-22
Dave05-May-22

我想将表_a的名称和日期列传递到以下代码为值。整个代码在SAS中运行。

proc sql;
select table_A.name, table_A.date, count(distinct(table_B.shipments)) as cul_count
from table_B 
where table_B.names = table_A.name
and table_B.dates between (table_A.date and date());/*date () is to get today's date*/
quit;

我想传递table_a值。有没有使用连接的方法可以做到这一点?尝试学习动态/参数编码。 无论如何使用加入? 预期输出如下,

名称日期CUL_COUNT
MARK29-APR-2265
DAVE05-MAY-22 102102

I have a table_A. Sample data for table_A with name and dates

namedate
Mark29-APR-22
Dave05-May-22

I would like to pass the name and date column of table_A to the following code as values. The whole code is running in SAS.

proc sql;
select table_A.name, table_A.date, count(distinct(table_B.shipments)) as cul_count
from table_B 
where table_B.names = table_A.name
and table_B.dates between (table_A.date and date());/*date () is to get today's date*/
quit;

I want to pass table_A values. Is there a way to do this without using joins? Trying to learn dynamic/parametric coding.
And anyway with using joins?
The expected output is as follows

namedatecul_count
Mark29-APR-2265
Dave05-May-22102

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

陪你到最终 2025-02-03 09:13:34
  • 使用正确的加入将数据限制在“右”表中,然后加入名称,并指定日期应在(包括)表A。

    中的日期之后(包括)

  • 。 /tb。

  • 对变量的表引用需要应用于变量而不是函数(即tb。

  • 求和,您想通过通常

    包括一个组


proc sql;
select ta.name, ta.date, count(distinct(tb.shipments)) as cul_count
from table_B tb
right join table_A ta
on tb.name=ta.name
and ta.date >= tb.date
group by ta.name, ta.date;
quit;

编辑来包括一个组:根据您的评论
这是您可以动态构建的查询的手动版本:

proc sql;
select tb.name, min(tb.date) as date, count(distinct(tb.shipments)) as cul_count
from table_B tb
where (name='Mark' and date>'29APR2022'd)
or (name = 'Dave' and date>'05May2022'd )
group by tb.name;
quit;

这是我最初的尝试。首先,您必须弄清楚如何手动构建上面的零件,然后可以使用Call Execute根据需要构造代码。但是上面的部分是关键。未经测试的括号可能会被关闭,以用于说明目的。

data _null_;
set table_A end=eof;

*runs only for first record;
if _n_=1 then do;
call execute('proc sql;
select tb.name, min(tb.date) as date, count(distinct(tb.shipments)) as cul_count
from table_B tb
where ');
end;

*runs for every record;
str = catt('(name = ', quote(name), ' and date > ' , date, ')');
call execute(str);

*runs for every record except for the last one;
if not  eof then call execute(' or ');

*runs only for last record;
if eof then call execute(' group by tb.name;quit;');

run;

  • Use a right join to limit the data to the 'right' table and then join on the names and specify that the dates should be after (including) the date in table A.

  • Alias make it simpler to read the code, ta/tb.

  • Table references to variables need to apply to the variable not the function (ie tb.shipments not tb.count).

  • For summations, you want to include a GROUP BY typically


proc sql;
select ta.name, ta.date, count(distinct(tb.shipments)) as cul_count
from table_B tb
right join table_A ta
on tb.name=ta.name
and ta.date >= tb.date
group by ta.name, ta.date;
quit;

EDIT: based on your comment
This is a manual version of the query you could build dynamically:

proc sql;
select tb.name, min(tb.date) as date, count(distinct(tb.shipments)) as cul_count
from table_B tb
where (name='Mark' and date>'29APR2022'd)
or (name = 'Dave' and date>'05May2022'd )
group by tb.name;
quit;

Here's my initial attempt at it. First you have to figure out how to manually build the part above, then you can use CALL EXECUTE to construct the code as desired. But the part above is key. Untested, so parentheses may be off, included for illustration purposes.

data _null_;
set table_A end=eof;

*runs only for first record;
if _n_=1 then do;
call execute('proc sql;
select tb.name, min(tb.date) as date, count(distinct(tb.shipments)) as cul_count
from table_B tb
where ');
end;

*runs for every record;
str = catt('(name = ', quote(name), ' and date > ' , date, ')');
call execute(str);

*runs for every record except for the last one;
if not  eof then call execute(' or ');

*runs only for last record;
if eof then call execute(' group by tb.name;quit;');

run;

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