全部版块 我的主页
论坛 数据科学与人工智能 数据分析与数据科学 SAS专版
2244 2
2013-10-02
本人SAS菜鸟一枚,十一假期,闲来没事就在CNKI上面找了一篇文章《Top sis 法在SAS 宏中的统计实现》,想要学习一下SAS宏,但是一直出错,特别是%let语句定义宏变量以及%let语句中镶嵌引用宏变量,请求各位大侠帮忙修改一下下面这段程序,下附原参考文献和自己从里面弄得程序: WARNING: Apparent symbolic reference VAR not resolved.ERROR: A character operand was found in the %EVAL function or %IF condition where a numerc       operand is required. The condition was: & &var&i.=ERROR: The condition in the %DO %UNTIL loop, & &var&i.=, yielded an invalid or missing value, .       The macro will stop executing.
%macro topsis(database=,vara=,vark=,weight=0);/*database:被分析变量所在的数据集   
vara:要分析的变量   
vark:结果中保留的变量     
weight:权重值可取0和1,默认去0*/
%let i=1;%let var&i.=%scan(&vara.,&i.,'');
%do %until(& &var&i.=);   
%let i=%eval(&i.+1);
%let var&i.=%scan(&vara.,&i.,'');
%end;
%let var_num=%eval(&i.-1);


%if &weight. %then %do;   
data top_1 top_2;      
set &database. end=last;      
if last then output top_2;      
else output top_1;      
run;      
%end;
%else %do;  
data top_1;      
set &database.;      
data top_2;        
%do i=1 %to &var_num.;        & &var. &i.;        %end;        run;        %end;
data _null_;set top_1 end=last;
if last then call symput('obj_num',compress(_n_));
%let sqr_str=;
data top_a;set top_1 end=last;%do i=1 %to &var_num.;  
& &var. &i.._2=& &var.&i.**2;   
%let sqr_str=&sqr_str & &var&i..2;%end;

proc transpose data=top_a(keep=&sqr_str.) prefix=top_m out=top_b;run;
proc transpose data=top_a(keep=&vara.) prefix=top_n out=top_c;run;
proc transpose data=top_2(keep=&vara.) prefix=top_w out=top_w;run;
data top_d;set top_b;z=sum(of top_m1-top_m&obj_num.);
%let min_str=;%let max_str=;
data top_e;merge top_c top_d top_w;%do i=1 %to &obi_num.;z&i.=top_n&i./sqrt(z);
%end;
max=max(of z1-z&obj_num.);min=min(of z1 z&obj_num.);
%do i=1 %to %obj_num.;d_max&i.=top_w1*(z&i.-max)**2;d_min&i.=top_w1*(z&i.-min)**2;
%let max_str=&max_str d_max&i;%let min_str=&max_str d_min&i;%end;
proc transpose data=top_e(keep=&max_str.) prefix=max out=top_f;run;
proc transpose data=top_e(keep=&min_str.) prefix=min out=top_g;run;
data top_h(keep=&vark. d_max d_min c);merge top_1 top_f top_g;
max_sum=sum(of max1-max&var_num.);
min_sum=sum(of min1-min&var_num.);

d_max=sqrt(max_sum);d_min=sqrt(min_sum);C=d_min/(d_max+d_min);
proc rank data=top_h descending out=topsis_result;ranks order;var c;run;
proc print;title "The Topsis Report of &vark.";run;
%mend topsis;
data SF236;input city $ x1-x8;
format city $ 5. x1-x8 6.2;
cards;
CS 72.07 64.08 75.40 46.85 69.10 78.83 81.78 75.63
SZ 74.58 63.17 68.04 53.07 70.87 76.57 71.86 68.91
SY 77.79 54.77 72.32 61.83 69.35 73.65 63.41 71.75
GZ 82.01 71.03 73.10 58.93 68.83 73.00 72.27 72.64
CD 79.78 79.06 78.27 56.77 70.85 82.57 82.96 77.54
TJ 66.37 59.91 73.16 69.27 79.09 67.92 68.14 57.12
SH 67.11 76.15 72.37 54.63 68.13 77.22 77.71 78.63
;
run;
proc format;
value $ cityfmt CS='ChangSha'                SZ='SuZhou'                                SY='ShenYang'                                GZ='GuangZhou'                                CD='ChengDu'                                TJ='TianJin'                                SH='ShangHai';run;
proc print data=SF236;format city $cityfmt.;title "Data of SF236";run;
%topsis(database=SF236,vara=x1-x8,vark=city,weight=0);



二维码

扫码加我 拉你入群

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

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

全部回复
2013-10-2 22:45:52
dude, copy it from here http://www.yixue68.com/jiaoyu/html/?21856.html and save yourself lots of trouble
二维码

扫码加我 拉你入群

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

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

2013-10-2 23:03:47
邓贵大 发表于 2013-10-2 22:45
dude, copy it from here http://www.yixue68.com/jiaoyu/html/?21856.html and save yourself lots of tro ...

Thank u so much, an useful method to check the SAS macro in papers has been learnt.
二维码

扫码加我 拉你入群

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

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

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

说点什么

分享

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