Transpose long to wide in SAS

后端 未结 3 607
别跟我提以往
别跟我提以往 2021-01-28 18:53

I have a very large data set (18 million observations) that I would like to transpose by subsetting based on one variable and creating 900 new variables out of those sub/ets. Ex

3条回答
  •  既然无缘
    2021-01-28 18:57

    I'm not sure what your subsetting would be based on, but if it's only the id, it is rather straightforward.

    Using the example from this ucla's page on proc sort, your example would work fine using slightly modified input data:

    data long; 
      input id year faminc ; 
      datalines ; 
    1 96 40000 
    1 97 40500 
    1 98 41000 
    2 96 45000 
    2 97 45400 
    2 98 45800 
    3 96 75000 
    3 97 76000 
    3 98 77000 
    ; 
    
    proc sort data=long;
      by year;
    run;
    
    proc transpose data=long out=wide(drop=_name_) prefix=var; 
      by year; 
      var faminc; 
    run;
    

    Results

    year var1  var2  var3 
    96   40000 45000 75000 
    97   40500 45400 76000 
    98   41000 45800 77000 
    

    UCLA's SAS help pages are much clearer than SAS's own as regards to using proc transpose in both directions. Here are 4 valuable links...

    Long to Wide with Proc Transpose
    Wide to Long with Proc Transpose

    Long to Wide with Data Step
    Wide to Long with Data Step

提交回复
热议问题