首页 > 解决方案 > 如何通过观察数据步骤将我的数据转置在 sas 上

问题描述

我有一个类似这样的 sas 数据库:

id birthday Date1    Date2
1  12/4/01  12/4/13  12/3/14
2  12/3/01  12/6/13  12/2/14
3  12/9/01  12/4/03  12/9/14
4  12/8/13  12/3/14  12/10/16

我想要这种形式的数据:

id Date     Datetype
1  12/4/01  birthday  
1  12/4/13  1   
1  12/3/14  2    
2  12/3/01  birthday  
2  12/6/13  1
2  12/2/14  2
3  12/9/01  birthday
3  12/4/03  1
3  12/9/14  2
4  12/8/13  birthday
4  12/3/14  1
4  12/10/16 2

感谢您的帮助,我在第二周使用 sas <3 编辑:感谢我,我没有找到排序方法。

标签: sassas-studio

解决方案


再会。以下应该是您所追求的。我没有想出一种简单的方法来重命名列,因为它们不在起始数据中。

   /*Data generation for ease of testing*/
    data begin; 
        input id birthday $ Date1 $ Date2 $; 
        cards;
    1  12/4/01  12/4/13  12/3/14
    2  12/3/01  12/6/13  12/2/14
    3  12/9/01  12/4/03  12/9/14
    4  12/8/13  12/3/14  12/10/16
    ; run; 

   /*The trick here is to use date: The colon means everything beginning with date, comparae with sql 'date%'*/
    proc transpose data= begin out=trans; 
        by id; 
        var birthday date: ; 
    run;

    /*Cleanup. Renaming the columns as you wanted.*/
    data trans;
        set trans;
        rename _NAME_= Datetype COL1= Date;
    run; 

从肯特大学网站查看更多信息


推荐阅读