如何在 SAS 中将一张表变成一长排? [英] How do you make a table into one long row in SAS?

查看:11
本文介绍了如何在 SAS 中将一张表变成一长排?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含许多变量的表格,例如:

I have a table with a number of variables such as:

+-----------+------------+---------+-----------+--------+
| DateFrom  |   DateTo   |  Price  | Discount  |  Cost  |
+-----------+------------+---------+-----------+--------+
| 01jan17   |  01jul17   |     17  |        4  |     5  |
| 01aug17   |  01feb18   |    15   |       1   |     3  |
| 01mar18   |  01dec18   |    12   |       2   |     1  |
| ...       |  ...       |    ...  |      ...  |    ... |
+-----------+------------+---------+-----------+--------+ 

但是我想拆分这个,所以我有:

However I want to split this so I have:

+------------+------------+----------+-------------+---------+-------------+------------+----------+-------------+-------------+
| DateFrom1  |  DateTo1   |  Price1  |  Discount1  |  Cost1  |  DateFrom2  |  DateTo2   |  Price2  |  Discount2  |  Cost2 ...  |
+------------+------------+----------+-------------+---------+-------------+------------+----------+-------------+-------------+
| 01jan17    |   01jul17  |      17  |          4  |      5  |  01aug17    |  01feb18   |     15   |         1   |          3  |
+------------+------------+----------+-------------+---------+-------------+------------+----------+-------------+-------------+

推荐答案

使用 proc summaryidgroup 语句有一个很酷(并不明显)的解决方案需要几行代码.这在内存中运行,如果数据集很大,您可能会遇到问题,否则效果很好.

There's a cool (not at all obvious) solution using proc summary and the idgroup statement that only takes a few lines of code. This runs in memory and you're likely to come into problems if the dataset is large, otherwise this works very well.

请注意,out[3] 与源数据中的行数有关.通过添加计算行数并将其存储在宏变量中的先前步骤,您可以轻松地实现动态化.

Note that out[3] relates to the number of rows in the source data. You could easily make this dynamic by adding a prior step that calculates the number of rows and stores it in a macro variable.

/* create initial dataset */
data have;
input (DateFrom DateTo) (:date7.) Price Discount Cost;
format DateFrom DateTo date7.;
datalines;
01jan17 01jul17 17 4 5
01aug17 01feb18 15 1 3
01mar18 01dec18 12 2 1
;
run;

/* transform data into 1 row */
proc summary data=have nway;
output out=want (drop=_:) 
        idgroup(out[3] (_all_)=) / autoname;
run;

这篇关于如何在 SAS 中将一张表变成一长排?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆