全部版块 我的主页
论坛 数据科学与人工智能 数据分析与数据科学 SAS专版
2945 5
2010-01-21
需要做一个宏,这个宏的作用是能对不同的数据集求相关系数并最终的出相关性最高的一对数据。我已经做出了初步的引入数据的处理,下面的code能够得到的结果是一个相关系数的表格:
%macro corr(lib,table);
  %if &table= %then %do;
      %put 'la table n''est pas renseigne';  
      /*%goto exit;*/
      %end;
  %else %if &lib= %then
      %do;
        %put 'la librairie n''est pas renseigne';   
        proc corr data=&table plots=matrix outp=corr;
        run;
      %end;
    %else
      %do;
         proc corr data=&&lib..&table plots=matrix outp=corr;
         run;
      %end;

data corr;
set corr;
if _name_='' then delete;
drop _type_;
run;

%mend corr;

问题1:我尝试了不同的数据集,但是发现一个问题:这个宏直接省略了字符型(qualitative)数据,只保留了数值型(quantitative)数据。应该怎么处理呢?

问题2:这里的macro参数我只设置了lib和table两个。再往下做的话,还需要一个参数var,也就是要指定研究的变量(考察的变量的个数肯定要大于等于2,但是也没有上限,视数据集情况而定)。对于不同大小不同类型的数据集,应该怎么设定这个参数呢?if first.type then num=1;else num=num+1;if type=1
then
do;call symput(...)我试了一下这个语句,但是只能用于数值型数据,没办法用在字符型数据上。



求高人解答!
谢谢了!
二维码

扫码加我 拉你入群

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

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

全部回复
2010-1-21 09:06:16
elliott828 发表于 2010-1-21 08:46
需要做一个宏,这个宏的作用是能对不同的数据集求相关系数并最终的出相关性最高的一对数据。我已经做出了初步的引入数据的处理,下面的code能够得到的结果是一个相关系数的表格:
%macro corr(lib,table);
  %if &table= %then %do;
      %put 'la table n''est pas renseigne';  
      /*%goto exit;*/
      %end;
  %else %if &lib= %then
      %do;
        %put 'la librairie n''est pas renseigne';   
        proc corr data=&table plots=matrix outp=corr;
        run;
      %end;
    %else
      %do;
         proc corr data=&&lib..&table plots=matrix outp=corr;
         run;
      %end;

data corr;
set corr;
if _name_='' then delete;
drop _type_;
run;

%mend corr;

问题1:我尝试了不同的数据集,但是发现一个问题:这个宏直接省略了字符型(qualitative)数据,只保留了数值型(quantitative)数据。应该怎么处理呢?

问题2:这里的macro参数我只设置了lib和table两个。再往下做的话,还需要一个参数var,也就是要指定研究的变量(考察的变量的个数肯定要大于等于2,但是也没有上限,视数据集情况而定)。对于不同大小不同类型的数据集,应该怎么设定这个参数呢?if first.type then num=1;else num=num+1;if type=1
then
do;call symput(...)我试了一下这个语句,但是只能用于数值型数据,没办法用在字符型数据上。



求高人解答!
谢谢了!
1) 这个宏直接省略了字符型(qualitative)数据 ---- there is no defined correlation among 字符型's.
2) combine lib and table name as one argument   --- dsn=mylib.mydata   
3) consider add drop options to drop the variables one does not need correlations(example, id variable --- no numerical meanings at all)
4) add a where option in case one needs to run it in a sub-sample or a smaller sample.
二维码

扫码加我 拉你入群

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

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

2010-1-21 18:09:02
2# bobguy 第四条我不太理解。
你的意思是不是让我弄出一个表格出来,表格里有所有的变量名。然后把这个表设定成&var?
比如说我在设定参数的时候写:%macro corr(lib,table,var)
关于变量的参数只有var一个,如果我要从中选取两个变量,那我应该怎么调用macro呢?


还有就是有时候字符型数据也是可以跟数值型数据产生关系的啊(比如说性别跟头发长度,性别跟身高),不能一概都删掉吧

谢谢了
二维码

扫码加我 拉你入群

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

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

2010-1-22 04:43:48
3# elliott828
复制代码
If you need all the correlations between any two continuous variables, you can specify ‘_numeric_’ for macro parameter 'VAR'.
You may need to read through the book again, as BOBGUY indicated above, proc CORR is only for continuous variables, which have to be numeric in SAS data set.
By the way, BOBGUY really have taught us a lot about SAS and statistics. Also AiMeng爱萌 alike. Thanks again.

JingJu
二维码

扫码加我 拉你入群

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

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

2010-1-22 08:53:59
elliott828 发表于 2010-1-21 18:09
2# bobguy 第四条我不太理解。
你的意思是不是让我弄出一个表格出来,表格里有所有的变量名。然后把这个表设定成&var?
比如说我在设定参数的时候写:%macro corr(lib,table,var)
关于变量的参数只有var一个,如果我要从中选取两个变量,那我应该怎么调用macro呢?


还有就是有时候字符型数据也是可以跟数值型数据产生关系的啊(比如说性别跟头发长度,性别跟身高),不能一概都删掉吧

谢谢了
"还有就是有时候字符型数据也是可以跟数值型数据产生关系的啊(比如说性别跟头发长度,性别跟身高),不能一概都删掉吧"

That is different concept here. The distribution of height may be gender dependent. You may compare the means and deviation by gender, but not correlations. You may measure the correlation between weight and height in male or in female separately.

*************************************************;
Here is an example to illustrate my comments.

%macro corr(dsn=, drop=, where=, odsn=corr);
  
  %if %length(&drop)=0 %then %let dp=%str( );
  %else %let dp=(drop=&drop);

  %if %length(&where)=0 %then %let whereit=%str( );
  %else %let whereit=&where %str(;);

     proc corr data=&dsn &dp  outp=&odsn(where=(_type_='CORR')) noprint;
     &whereit
     run;

  %mend;

%corr(dsn=sashelp.class, drop=, where=, odsn=corr2);
proc print; run;
%corr(dsn=sashelp.class, drop=, where=where ranuni(-1) <0.3, odsn=corr2);
proc print; run;
二维码

扫码加我 拉你入群

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

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

2010-1-23 06:24:31
等我完全修改完成后我将把我做好的macro贴上来。:)
二维码

扫码加我 拉你入群

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

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

相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群