SAS 宏使用列表迭代过程频率

问题描述 投票:0回答:1

我有一个数据集,我必须在其中运行十几个组织的十几个变量的频率。这对于 proc print 来说非常容易,但我不想更新 WHERE 语句十几次来运行每个频率,所以我希望用宏来简化它。

作为示例,我们可以使用 sashelp.cars 数据集,其中每个汽车制造商运行两个变量的频率(为了简单起见)。

我编写了一个宏,希望迭代一个简单的过程频率,如下所示,除了该宏将迭代 WHERE 语句中的汽车制造商“make”列表并运行指定的频率。

proc freq data=sashelp.cars; 
    table Type DriveTrain;
    where make = "Honda";
run;

这是我写的宏:

%macro car_freq(dataset, n_make);
    %do i = 1 %to &n_make;
        %let maker =  %scan(make., &i);

        title "&maker";
    proc freq data=&dataset;
        where make = "&maker";
        tables type DriveTrain;
    run;
title;
%end;
%mend car_freq;

%car_freq(sashelp.cars, 38)

当我运行宏时,它运行时没有错误,但表示每次迭代都有 0 个观察值。为了让它迭代制造商列表并运行两个变量的频率,我缺少什么?

loops sas macros frequency
1个回答
0
投票

可以对数据进行排序并使用BY语句。

proc sort data=sashelp.cars out=cars;
  by make;
run;

ods html file='report.html';

  options byline;
  title;
  proc freq data=cars;
    by make;
    tables type drivetrain;
    where make in: ('L');
  run; 

ods html close;

将会输出

© www.soinside.com 2019 - 2024. All rights reserved.