我想使用SASHELP.CARS,按原产地进行PROC TABULATE。因此,第一种方法是制作3个PROC TABULATE,例如:
PROC TABULATE DATA = data out=tabulate;
where Origin="Asia";
CLASS Make DriveTrain ;
TABLE (Make), (DriveTrain) / nocellmerge ;
run;
但是,相反,我想在宏循环中自动执行此操作(这是我做的一个简单示例。我使用的实际数据库更加复杂;这就是为什么我需要创建宏:)的原因。您能否帮助我,为什么以下代码不起作用?似乎是问题所在的« where Origin = reg; »部分。谢谢 !所以这是我的代码:
data data; set sashelp.cars;run;
data classes;
input id_reg reg_name $ ;
cards;
1 Asia
2 Europe
3 USA
run;
%macro comp;
%local i reg;
%do i=1 %to 3;
proc sql ;
select reg_name
into
:reg_name
from classes
where id_reg = &i.;
quit;
%let reg=reg_name;
PROC TABULATE DATA = data out=tabulate_&i;
where Origin=reg;
CLASS Make DriveTrain ;
TABLE (Make), (DriveTrain) / nocellmerge ;
run;
%end;
%mend comp;
%comp
答案 0 :(得分:1)
如果您坚持使用Macro,将双引号宏变量的解析度生成正确的语句,以便将字符串文字注入到提交流中。
where Origin="®";
答案 1 :(得分:0)
使用BY
语句像数据集的分组子集那样独立处理。
使用WHERE
语句选择要处理的子集。
示例:
ods html file='output.html' style=plateau;
proc sort data=sashelp.cars out=cars_sorted;
by origin;
run;
title;footnote;
options nocenter nodate nonumber;
PROC TABULATE DATA=cars_sorted;
by origin;
where Origin in ("Asia", "Europe", "USA");
where also make >= 'P'; * further subset for reduced size of output screen shot;
CLASS Make DriveTrain ;
TABLE (Make), (DriveTrain) / nocellmerge ;
run;
ods html close;
或者,使用TABLE
形式的<page dimension>,<row dimension>,<column dimension>
语句代替BY
组处理。这种格式不需要预先排序的数据,因为格式是根据CLASS
变量构造的。
示例:
PROC TABULATE DATA=sashelp.cars; /* original data, not sorted */
where Origin in ("Asia", "Europe", "USA");
where also make >= 'P'; * further subset for reduced size of output screen shot;
CLASS Origin Make DriveTrain ; /* Origin added to CLASS */
TABLE Origin, (Make), (DriveTrain) / nocellmerge ; /* Origin is page dimension */
run;
答案 2 :(得分:0)
非常感谢!这是有效的代码:
data data; set sashelp.cars;run;
data classes;
input id_reg reg_name $ ;
cards;
1 Asia
2 Europe
3 USA
run;
%macro comp;
%local i ;
%do i=1 %to 3;
proc sql ;
select reg_name
into
:reg_name
from classes
where id_reg = &i.;
quit;
PROC TABULATE DATA = data(where=(Origin="®_name")) out=tabulate_&i;
CLASS Make DriveTrain ;
TABLE (Make), (DriveTrain) / nocellmerge ;
run;
%end;
%mend comp;
%comp