按原点生成 Proc Tabulate 的宏循环

Macro loop to generate Proc Tabulate by origin

使用 SASHELP.CARS,我想按 Origin 制作一个 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

使用 BY 语句独立处理数据集的分组子集。 使用 WHERE 语句 select 要处理的子集。

示例:

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;

输出

或者,使用形式为 <page dimension>,<row dimension>,<column dimension>TABLE 语句代替 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;

输出

如果您坚持使用宏,将通过双引号宏变量解析生成正确的语句,从而将字符串文字注入提交流。

  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 ;

    %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="&reg_name")) out=tabulate_&i;
        CLASS Make DriveTrain   ; 
        TABLE (Make), (DriveTrain)   / nocellmerge ; 
        run;

    %end;
%mend comp;
%comp