SAS proc转置并输出到excel
我的另一个 SAS 问题(我注意到这些不经常出现在这里......):
我有一个数据集,包含这样的内容:
Name | Category | Level | Score
John | cat1 | 1 | 80
John | cat1 | 2 | 70
John | cat1 | 3 | 10
John | cat2 | 1 | 60
John | cat2 | 2 | 95
John | cat2 | 3 | 43
John | cat2 | 4 | 28
输出(excel 格式)应该如下所示:
| cat1 | cat2 |
name | 1 | 2 | 3 | 1 | 2 | 3 | 4 |
John | 80 | 70 |10 |60 |95 |43 |28 |
我现在所做的,是使用proc 转置
以正确的顺序获取数据,然后 proc 导出
转到 .xls。
除了一件事之外,这工作得很好。我无法让第二层细分工作。所以现在,在我的 proc transpose
之前,我实际上在数据集中连接了我的类别和级别(例如将其设为“1_cat1”),然后对该值进行转置,给出以下输出:
name | 1_cat1 | 2_cat1 | 3_cat1 | 1_cat2 | 2_cat2 | 3_cat2 | 4_cat2 |
John | 80 | 70 | 10 | 60 | 95 | 43 | 28 |
Is有什么方法可以获得第一个所需的输出?
Another SAS question from me (I noticed these don't come up here that often...):
I have a data set containing something like this:
Name | Category | Level | Score
John | cat1 | 1 | 80
John | cat1 | 2 | 70
John | cat1 | 3 | 10
John | cat2 | 1 | 60
John | cat2 | 2 | 95
John | cat2 | 3 | 43
John | cat2 | 4 | 28
And the output (excel format) should look like:
| cat1 | cat2 |
name | 1 | 2 | 3 | 1 | 2 | 3 | 4 |
John | 80 | 70 |10 |60 |95 |43 |28 |
What I do now, is using proc transpose
to get the data in the right order and then proc export
to go to .xls.
This works fine, except for one thing. I cant get the second layer of subdivision to work. So right now, before my proc transpose
I actually concat my category and level in my dataset (eg making it '1_cat1') and then transpose on this value, giving me the following output:
name | 1_cat1 | 2_cat1 | 3_cat1 | 1_cat2 | 2_cat2 | 3_cat2 | 4_cat2 |
John | 80 | 70 | 10 | 60 | 95 | 43 | 28 |
Is there any way to get the first, desired output ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设您熟悉将其导入 Excel 的
ODS
选项(我只是懒惰地使用 html 并将其另存为 .xls,但您可以使用标记集等代替),这里是PROC REPORT
解决方案以您正在寻找的格式显示数据。查看 proc 报告中across
变量的使用情况。可能有一种方法可以抑制输出中 cat1 下未使用的列,但我现在不记得了。Assuming that you're familiar with your
ODS
options to get it into Excel (I'm just lazily using html and saving it as .xls, but you could use tagsets etc. instead), here is aPROC REPORT
solution to display the data in the format you're looking for. Check out the use ofacross
variables in proc report. There's probably a way to suppress the column that isn't used under cat1 in the output, but I can't recall it right now.我认为您无法使用 proc transpose 直接进入所需的输出,因为您希望让每个类别跨越多个级别。您可能想研究另外两个过程:报告和制表。我相信您可以直接从其中任何一个执行此操作,但自从我使用这些以来已经很多年了。第三种选择是使用 ODS 创建一个 XML 文件,您可以在其中几乎精确地控制您想要的输出显示方式,尽管需要花费更多的精力来学习如何执行此操作。
I don't think you will be able to go directly to your desired output using proc transpose since you are looking to get each category to span multiple levels. You might want to research two other procedures, REPORT and TABULATE. I believe you can do this directly from either one, but it has been years since I used these. A third option is to create an XML file with ODS in which you can control pretty much exactly how you want to output to appear, though it takes a little more effort to learn how to do this.