SAS:使用数组来转表 [英] SAS: Using an array to transpose a table

查看:162
本文介绍了SAS:使用数组来转表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图重现一些code这做的是什么第9-10页上的 http://support.sas.com/resources/papers/proceedings10/158-2010.pdf 。因此,而不是做一个表从广角到长,我想它长期成为宽。

I'm attempting to recreate some code which does the opposite of what is on page 9-10 on http://support.sas.com/resources/papers/proceedings10/158-2010.pdf. So instead of making a table go from wide to long, I'd like it to become long to wide.

Id Col1  Col2     
1  Val1  A    
1  Val2  B    
2  Val1  C    
2  Val3  D  
3  Val2  E 

转置到:

Id X_Val1 X_Val2 X_Val3  
1  A      B      .
2  C      .      D
3  .      .      E

这是我应该如何去这个任何想法?我知道我应该使用一个数组,并试图建立一个新的列X_Val1,其中X_Val1 =猫('X',VAL1),其中X是一些字符串。

Any ideas on how I should be going about this? I know I should be using an array and trying to create a new column X_Val1 where X_Val1 = cat('X',Val1) where X is some string.

推荐答案

您需要先弄清楚你有多少变量需要。然后,您可以创建变量,使用数组,并指定值。

You need to first figure out how many variables you need. Then you can create the variables, use an array, and assign the values.

data test;
input id col1 $ col2 $;
datalines;   
1  Val1  A    
1  Val2  B    
2  Val3  C    
2  Val4  D   
2  Val5  E
;
run;

/*Need to get the number of variables that need to be created*/
proc sql noprint;
select max(c)
    into :arr_size
    from
    ( select ID, count(*) as c
        from test
        group by id
    );
quit;

/*Get rid of leading spaces*/
%let arr_size=%left(&arr_size);
%put &arr_size;

data test_t;
set test;
by id;
/*Create the variables*/
format SOME_X1 - SOME_X&arr_size $8.;
/*Create an array*/
array SOME_X[&arr_size];

/*Retain the values*/
retain count SOME_X:;

if first.id then do;
count = 0;
do i=1 to &arr_size;
    SOME_X[i] = "";
end;
end;

count = count + 1;
SOME_X[count] = col2;

if last.id then
    output;

keep id SOME_X:;
run;

这篇关于SAS:使用数组来转表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆