SAS proc转置并输出到excel

发布于 2024-11-05 03:35:09 字数 1000 浏览 4 评论 0原文

我的另一个 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 exportto 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 技术交流群。

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

发布评论

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

评论(2

叫思念不要吵 2024-11-12 03:35:09

假设您熟悉将其导入 Excel 的 ODS 选项(我只是懒惰地使用 html 并将其另存为 .xls,但您可以使用标记集等代替),这里是PROC REPORT 解决方案以您正在寻找的格式显示数据。查看 proc 报告中 across 变量的使用情况。可能有一种方法可以抑制输出中 cat1 下未使用的列,但我现在不记得了。

data testData;
  infile datalines dsd delimiter='|';
  input name $ category $ level score;
  datalines;
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
;
run;

ods html file="C:\SomePath\MyFile.xls";

proc report
  data=testData;
  columns name category,level,score;
  define name / group;
  define category / across '';
  define level / across '';
  define score / sum '';
run;

ods html close;

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 a PROC REPORT solution to display the data in the format you're looking for. Check out the use of across 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.

data testData;
  infile datalines dsd delimiter='|';
  input name $ category $ level score;
  datalines;
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
;
run;

ods html file="C:\SomePath\MyFile.xls";

proc report
  data=testData;
  columns name category,level,score;
  define name / group;
  define category / across '';
  define level / across '';
  define score / sum '';
run;

ods html close;
内心荒芜 2024-11-12 03:35:09

我认为您无法使用 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.

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