楼主: elliott828
4961 12

求教:SAS表格格式转换 [推广有奖]

  • 0关注
  • 0粉丝

本科生

20%

还不是VIP/贵宾

-

威望
0
论坛币
1 个
通用积分
0
学术水平
0 点
热心指数
0 点
信用等级
0 点
经验
890 点
帖子
57
精华
0
在线时间
58 小时
注册时间
2009-5-7
最后登录
2016-6-5

楼主
elliott828 发表于 2010-1-16 20:54:47 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
我用proc corr得到下面的表格,并用ods output导出到work里的表格里:
            Pearson Correlation Coefficients
            Prob > |r| under H0: Rho=0
            Number of Observations

                Age            Weight           Oxygen         RunTime

Age       1.00000      -0.23354        -0.31474         0.14478
                 0.2061        0.0963          0.4536
                    31              31                29                29

Weight   -0.23354      1.00000        -0.15358       0.20072
                 0.2061                              0.4264        0.2965
                   31               31                 29               29

Oxygen  -0.31474      -0.15358        1.00000       -0.86843
                   0.0963        0.4264                             <.0001
                    29               29                 29              28

RunTime  0.14478        0.20072       -0.86843       1.00000
                  0.4536        0.2965         <.0001
                      29            29                   28              29

我现在只想保留相关系数的数据,去掉p-value和number of observation的数据,并做成下面的这种格式:
couple of variables      corr
Age*Weight                 -0.23354
Age*Oxygen                -0.31474
Age*RunTime             0.14478
...                                  ...
每一对变量名不要重复。
请问哪位高人能给个提示或者给个code?
谢谢啦
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:格式转换 coefficients observations correlation coefficient 格式转换

沙发
bobguy 发表于 2010-1-16 22:24:17
elliott828 发表于 2010-1-16 20:54
我用proc corr得到下面的表格,并用ods output导出到work里的表格里:
            Pearson Correlation Coefficients
            Prob > |r| under H0: Rho=0
            Number of Observations

                Age            Weight           Oxygen         RunTime

Age       1.00000      -0.23354        -0.31474         0.14478
                 0.2061        0.0963          0.4536
                    31              31                29                29

Weight   -0.23354      1.00000        -0.15358       0.20072
                 0.2061                              0.4264        0.2965
                   31               31                 29               29

Oxygen  -0.31474      -0.15358        1.00000       -0.86843
                   0.0963        0.4264
1) Use the outp option instead of ODS to get correlation matrix.
2) Get what in a data step as below.

HTH

proc corr data=sashelp.class outp=pearson(where=(_TYPE_='CORR'));
var age height weight;
run;

data need_corr;
   set pearson;
   array corr(*)   Height     Weight ;
   i=_n_;
   do j=i to dim(corr);
       corr_name=compress(_name_||'*'||vname(corr(j)) );
       corrcoef= corr(j);
       output;
    end;
    if i=dim(corr) then stop;
    keep  corr_name      corrcoef ;
run;

proc print data=need_corr; run;

藤椅
爱萌 发表于 2010-1-16 23:02:45
proc report
最恨对我说谎或欺骗我的人

板凳
elliott828 发表于 2010-1-17 05:46:36
2# bobguy


谢谢你bobguy!
但是按照你的code做出来结果是一个只有三个变量关系的表格:
1.jpg

data pearson;
set corr1;
array corr(*) age weight;
i=_n_;
do j=i to dim(corr);
corr_name=compress(_name_||'*'||vname(corr(j)));
corrcoef=corr(j);
output;
end;
if i=dim(corr) then stop;
run;

这是我用的code,稍微改了一下变量。

如何才能按照这种格式显示所有变量间的相关系数呢?

另外,如果把这个code应用到macro里面,如何处理变量名呢?我的意思是,对不同的数据,变量的名字和数量都不一样,如何用一种code来处理所有数据呢?

谢谢啦!
la croyance

报纸
bobguy 发表于 2010-1-17 06:55:08
elliott828 发表于 2010-1-17 05:46
2# bobguy


谢谢你bobguy!
但是按照你的code做出来结果是一个只有三个变量关系的表格:


data pearson;
set corr1;
array corr(*) age weight;
i=_n_;
do j=i to dim(corr);
corr_name=compress(_name_||'*'||vname(corr(j)));
corrcoef=corr(j);
output;
end;
if i=dim(corr) then stop;
run;

这是我用的code,稍微改了一下变量。

如何才能按照这种格式显示所有变量间的相关系数呢?

另外,如果把这个code应用到macro里面,如何处理变量名呢?我的意思是,对不同的数据,变量的名字和数量都不一样,如何用一种code来处理所有数据呢?

谢谢啦!
Suppose you have 5 variables namely, x1 x2 x3 x4 x5

The information you want is the up-diagonal elements.

1) outp=pearson(where=(_TYPE_='CORR')); ** this where is needed to keep only information about correlations.
2) array corr(*)   Height     Weight ; ***change this into array corr(*)   x2 x3 x4 x5 ;

地板
elliott828 发表于 2010-1-17 07:21:49
5# bobguy 实在是很感谢!大致明白了!
我现在是要做一个macro。先尝试一下用普通code弄出来的样子:)
十分感谢!
la croyance

7
bobguy 发表于 2010-1-17 07:33:09
elliott828 发表于 2010-1-17 07:21
5# bobguy 实在是很感谢!大致明白了!
我现在是要做一个macro。先尝试一下用普通code弄出来的样子:)
十分感谢!
Learn walking first, then running!

Have a good Day.

8
elliott828 发表于 2010-1-17 07:36:31
7# bobguy

没办法,不是我想学跑,是没时间学走路。。。
才接触这个很短的时间,就要求做macro的project了。。。
汗!

以后请多指教!
la croyance

9
bobguy 发表于 2010-1-18 00:29:41
bobguy 发表于 2010-1-17 06:55
elliott828 发表于 2010-1-17 05:46
2# bobguy


谢谢你bobguy!
但是按照你的code做出来结果是一个只有三个变量关系的表格:


data pearson;
set corr1;
array corr(*) age weight;
i=_n_;
do j=i to dim(corr);
corr_name=compress(_name_||'*'||vname(corr(j)));
corrcoef=corr(j);
output;
end;
if i=dim(corr) then stop;
run;

这是我用的code,稍微改了一下变量。

如何才能按照这种格式显示所有变量间的相关系数呢?

另外,如果把这个code应用到macro里面,如何处理变量名呢?我的意思是,对不同的数据,变量的名字和数量都不一样,如何用一种code来处理所有数据呢?

谢谢啦!
Suppose you have 5 variables namely, x1 x2 x3 x4 x5

The information you want is the up-diagonal elements.

1) outp=pearson(where=(_TYPE_='CORR')); ** this where is needed to keep only information about correlations.
2) array corr(*)   Height     Weight ; ***change this into array corr(*)   x2 x3 x4 x5 ;
I add a little bit in details

A sas table can be think of two dimesional arrays or matrix {i,j}

when one needs elements of off diagnal up-top of a correaltion matrix, one only needs to read (n^2-n)/2 elements since it is symmetric. n is for the number of variance elements and 1/2 is for  symmetric.

So the column index goes from 2 to n having n-1 elements. That is,
array corr(*)   Height     Weight ; **** start from second one in correaltion matrix.
The row index goes from 1 to n-1 having n-1 elements. That is SAS data set internal loop.
i=_n_;
if i=dim(corr) then stop;    *** The last row having no information for correaltion.

Hope this is clear.

10
小陈陈 发表于 2010-1-18 06:50:03
解释得非常详细,感谢高人!

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2026-1-1 08:43