Transpose wide to long with dynamic variables

后端 未结 1 1924
死守一世寂寞
死守一世寂寞 2020-12-21 20:59

I\'ve got a wide dataset with each month listed as a column. I\'d like to transpose the data to a long format, but the problem is that my column names will be changing in t

相关标签:
1条回答
  • 2020-12-21 21:42

    Simply run the transpose procedure and provide only the by statement.

    I've updated your sample data to convert the months to numeric values (rather than character which can't be transposed). I've also changed them to use valid base-sas names by removing the hyphen.

    data have;
    input  subject $ "Jan10"n  "Feb10"n  "Mar10"n ;
    datalines;
    1   12   18  22 
    2   13   19  23
    ;
    run;
    

    Here's the transpose syntax you need, it will transpose all numeric variables by default:

    proc transpose data=have out=want;
      by subject;
    run;
    

    You could also do something more explicit, but still dynamic such as:

    proc transpose data=have out=want;
      by subject;
      var jan: feb: mar: ; * ETC;
    run;
    

    This would transpose all vars that begin with jan/feb/mar etc... Useful in case your table contains other numeric variables that you don't want to include in the transpose.

    0 讨论(0)
提交回复
热议问题