楼主: elliott828
2992 5

请教SAS宏的问题 [推广有奖]

  • 0关注
  • 0粉丝

本科生

20%

还不是VIP/贵宾

-

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

楼主
elliott828 发表于 2010-1-21 08:46:39 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

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

经管之家联合CDA

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

感谢您参与论坛问题回答

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

+2 论坛币
需要做一个宏,这个宏的作用是能对不同的数据集求相关系数并最终的出相关性最高的一对数据。我已经做出了初步的引入数据的处理,下面的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(...)我试了一下这个语句,但是只能用于数值型数据,没办法用在字符型数据上。



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

扫码加我 拉你入群

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

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

关键词:SAS宏 Quantitative Qualitative QUANTITATIV matrix SAS 参数设定 字符型数据

回帖推荐

bobguy 发表于2楼  查看完整内容

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.

本帖被以下文库推荐

la croyance

沙发
bobguy 发表于 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.
已有 1 人评分经验 论坛币 收起 理由
bakoll + 3 + 3 精彩帖子

总评分: 经验 + 3  论坛币 + 3   查看全部评分

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


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

谢谢了
la croyance

板凳
jingju11 发表于 2010-1-22 04:43:48
3# elliott828

  1. %macro corrM(dsn, var);
  2.    %if %sysfunc(exist(&dsn)) %then
  3.       %do;
  4.        %put 'la librairie n''est pas renseigne';   %*?;
  5.    ods graphics on;
  6.          proc corr data=&dsn plots=matrix outp=corr(drop = _type_ where = (^missing(_name_)));
  7.        var &var;
  8.          run;
  9.       %end;
  10.     %else  %put 'la table n''est pas renseigne';
  11. quit;
  12. %mend corrM;

  13. %corrM(work.a, x1 x2)
复制代码
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

报纸
bobguy 发表于 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;

地板
elliott828 发表于 2010-1-23 06:24:31
等我完全修改完成后我将把我做好的macro贴上来。:)
la croyance

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

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2025-12-27 03:46