在SAS中迭代几个月
我有以下问题。这样的表中每个实体有六个月的交易数据:
customer_1 1000 January
customer_1 1000 January
customer_1 1000 February
customer_1 1000 February
customer_1 1000 March
customer_1 1000 April
customer_1 1000 May
customer_1 1000 June
我想计算每两个月的交易数量,例如:
1月至2月有4个交易数量。 2月至3月有3次交易 等等。
我使用嵌套环尝试,以便定义一个开始日期和结束日期,然后在循环结束时我添加了1个月并再次迭代。这样的事情:
do i = n to 1 by -1 while (date_key{i} >= begin_date);
do k = i to 1 by -1 while(date_key{k} >= b_d and date_key{k} <= intnx('Month',b_d,2, 's'));
if upcase(account{k}) in (&account_type) and
then do;
total_amount = sum(total_amount,currency_amount{k});
amt_cnt = amt_cnt + 1;
end;
end;
if total_amount >= threshold_total then m_cnt = sum(m_cnt,1);
b_d = intnx('Month',b_d,1,'s');
end;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能应该只使用多标签格式和支持它们的PROC。
首先,让我们创建一些实际的示例输入数据。
然后,让我们创建一个示例格式。这是一种名为“命名期”的格式,是2021年的前5个两个月期。
一旦定义了格式,将其应用于您的日期变量,并按日期进行汇总。
这是使用Proc摘要的代码。
和结果:
对于一系列日期,动态制作格式并不难。您只需要数据集中的这些变量即可与cntlin = proc格式的选项一起使用。
也许这样的东西:
You should probably just use a MULTILABEL format and a proc that supports them.
First let's create some actual sample input data.
Then let's create an example format. Here is a format named PERIODS that are the first 5 two month periods in 2021.
Once you have the format defined apply it to your DATE variable and summarize by date.
Here is code using PROC SUMMARY.
And the results:
It is not hard to make format dynamically for a range of dates. You just need these variables in a dataset to use with the CNTLIN= option of PROC FORMAT.
So perhaps something like this: