SAS宏可以打印多个报告

时间:2014-08-09 02:43:49

标签: macros sas

我有一个如下所示的数据集:

data have;
    input name $ class $ time score;
cards;
chewbacca wookie  1 97
chewbacca wookie 2 100
chewbacca wookie 3 95
saruman wizard 1 79
saruman wizard 2 85
saruman wizard 3 40
gandalf wizard 1 22
gandalf wizard 2 50
gandalf wizard 3 87
bieber canadian 1 50
bieber canadian 2 45
bieber canadian 3 10
;
run;

我正在创建一个执行两项操作的程序: 1.打印每个不同类的数据 2.为每个名称创建一个散点图x =时间y =得分。

执行以下代码将说明我想要的输出:

data chewbacca saruman gandalf bieber;
    set have;
    if name='chewbacca' then output chewbacca;
    else if name='saruman' then output saruman;
    else if name='gandalf' then output gandalf;
    else if name='bieber' then output bieber;
run;

title 'Report for wookie';
proc print data=have;
    where class='wookie';
run;
title 'Plot Chewbacca';
proc sgplot data=chewbacca;
    scatter x=time y=score;
run;
title 'Report for wizard';
proc print data=have;
    where class='wizard';
run;
title 'Plot Saruman';
proc sgplot data=saruman;
    scatter x=time y=score;
run;
title 'Plot Gandalf';
proc sgplot data=gandalf;
    scatter x=time y=score;
run;
title 'Report for canadian';
proc print data=have;
    where class='canadian';
run;
title 'Plot Bieber';
proc sgplot data=bieber;
    scatter x=time y=score;
run;

理想情况下,我想自动完成此操作。我一直试图设置它,但我错过了一些东西。这是我的尝试:

proc sql;
    select count(distinct name) into :numname
    from have;
    %let numname=&numname;
    select distinct name into :name1 - :name&numname
    from have;
    select count(distinct class) into :numclass
    from have;  
    %let numclass=&numclass;
    select distinct class into :class1 - :class&numclass
    from have;
quit;

%macro printit;
%do i = 1 %to &numclass;
title 'Report for &&class&i';
proc print data=have;
    where class=&&class&i;
run;
/*insert sgplot here*/
%end;
%mend;
%printit;

请在这里帮忙。无法将语法排序....

感谢。

2 个答案:

答案 0 :(得分:6)

我看到4个问题。

  1. 宏只会在双引号内解析。单引号掩盖了分辨率。所以将标题声明更改为:

    title "Report for &&class&i";

  2. 类变量是一个字符串。您需要在where子句中引用字符串:

    where class="&&class&i";

  3. 您不需要生成单独的数据集。您可以在指定SGPLOT

    的数据时添加where子句

    proc sgplot data=have(where=(name="&&name&i"));

  4. 名称和类的数量不同,因此您需要两个循环。

  5. 编辑:同时查看SGPANEL和/或SGRENDER。您可以在1次通话中生成所有图表。

答案 1 :(得分:1)

print程序和大多数ODS程序支持按组处理,这可能会更简单,并根据您的需要节省大量时间。

proc sort data=have; by class;
proc print data=have;
    by class;
run;

proc sort data=have; by name;
proc sgplot data=have;
 by name;
 scatter x=time y=score;
run;
相关问题