【问题标题】:Macro not loading data set宏未加载数据集
【发布时间】:2015-07-08 15:39:23
【问题描述】:

以前的帖子:

Variable check and summary out

Macro that outputs table with testing results of SAS table

问题/问题

从之前的帖子中,我认为我能够运行宏并产生所需的结果。但是,在最终收到输出无法正常工作的报告后,我真的很困惑为什么我会收到缺少变量的错误。看起来好像数据集在子设置后没有被加载。我能够处理基本的汇总统计表,但是当我加载宏时,输出不起作用。

为什么数据集没有加载?宏是否需要某种类型的数据集?

注意:一个限制是我无法访问数据集,所以我必须发送代码才能运行,并且几天内不会得到结果。这是一个非常漫长而令人沮丧的过程,但我相信有些人可以理解。

导致问题的代码是宏(在代码的开头)和最后一段使用数据集调用宏。

错误日志:

代码:

# Filename : Census2007_Hawaii_BearingCoffee_BigIsland.sas

/******************************************************************
 Clearance Test Macro
    input_dataset  - desired dataset which variables are located
    output_dataset - an output table with test results
    variable_to_consider - list of variables to compute test on
*******************************************************************/

%macro clearance_test(input_dataset= ,output_dataset=, variable_to_consider=);

%let variable_to_consider=%cmpres(&variable_to_consider);
proc sql noprint;
  select count(*) into : obs_count from &input_dataset;
quit;
%let obs_count=&obs_count;

proc transpose data=&input_dataset out=&output_dataset prefix=top_;
    var &variable_to_consider; 
run;

data &output_dataset;
set &output_dataset end=eof;
    array top(*) top_&obs_count.-top_1;
    x=dim(top);
    call sortn(of top[*]);
    total=sum(of top[*]);

top_2_total=sum(top_1, top_2);
    if sum(top_1,top_2) > 0.9  * total then Flag90=1; else Flag90=0;
    if top_1 > total * 0.6 then Flag60=1; else Flag60=0;

keep total top_1 top_2 _name_ top_2_total total Flag60 Flag90;

run;
%mend mymacro;

/***********************************************************************/

*Define file path statics;
Libname def 'P:\Hawaii_Arita\John_Hawaii_Coffee\Datasets';
Libname abc "P:\Hawaii_Arita\John_Hawaii_Coffee\Datasets";
option obs=max;

/* Initialize database */

DATA def.Census2007_Hawaii_Coffee;
    SET abc.census2007_hawaii_SubSet_Coffee;
    **<create the variables used in the macro> **;
RUN;

/* Clearance Test Results */

%clearance_test(input_dataset=def.census2007_hawaii_SubSet_Coffee, output_dataset=test_data ,variable_to_consider= OIR OIRO ROA ROAO SProfit 
LProfit SProfitAcre LProfitAcre Profitable MachineandRent UtilityandFuel LaborH LaborO FertilizerandChem MaintandCustom 
Interest Tax Dep Others TFPE_cal operators workers operatorsandworkers)

一个完整/可验证的例子:

这已经在远程机器上进行了测试并且可以完美运行。

/* Create test data set*/
data business_data;
do firm = 1 to 3;
revenue = rand("uniform");
costs = rand("uniform");
profits = rand("uniform");
vcost = rand("uniform");
output;
end;
run;
/******************************************************************
Clearance Test Macro
input_dataset - desired dataset which variables are located
output_dataset - an output table with test results
variable_to_consider - list of variables to compute test on
*******************************************************************/
%macro clearance_test(input_dataset= ,output_dataset=, variable_to_consider=);
%let variable_to_consider=%cmpres(&variable_to_consider);
proc sql noprint;
select count(*) into : obs_count from &input_dataset;
quit;
%let obs_count=&obs_count;
proc transpose data=&input_dataset out=&output_dataset prefix=top_;
var &variable_to_consider;
run;
data &output_dataset;
set &output_dataset end=eof;
array top(*) top_&obs_count.-top_1;
x=dim(top);
call sortn(of top[*]);
total=sum(of top[*]);
top_2_total=sum(top_1, top_2);
if sum(top_1,top_2) > 0.9 * total then Flag90=1; else Flag90=0;
if top_1 > total * 0.6 then Flag60=1; else Flag60=0;
keep total top_1 top_2 _name_ top_2_total total Flag60 Flag90;
run;
%mend mymacro;
/* Print summary table, run macro, and print clearance test table */
PROC MEANS data = business_data n sum mean median std;
VAR revenue costs profits vcost;
RUN;
%clearance_test(input_dataset=business_data, output_dataset=test_data ,
variable_to_consider=revenue costs profits vcost)
proc print data = test_data; run;

【问题讨论】:

  • 我删除了大部分代码,因为它与问题没有任何关系。
  • @Joe 谢谢!我只是想我应该删除子设置部分,但你删除了我认为相关的文件路径。
  • 文件路径几乎不相关:毕竟它们只在你的机器上有用,而且它们与错误无关[除非你使用了错误的路径,但我们怎么会知道这一点或能够提供帮助吗?] 并且数据步骤和后续 proc 方法是无关紧要的,因为它们不向宏提供任何输入。
  • 等等,也许数据步骤是相关的。您是否打算将数据步骤的结果作为宏的输入?因为目前不是,所以当前宏采用set 数据集(初始输入)。
  • @Joe 是的,我希望输入是作为子集的数据集。

标签: sas


【解决方案1】:

这是一个最小的、完整的可验证示例 (MCVE) 有助于测试您的问题是代码问题还是数据问题。

这是上面的代码,但带有 SASHELP 数据集(这些是 SAS 内置的,所以每个人都有)。

%macro clearance_test(input_dataset= ,output_dataset=, variable_to_consider=);

%let variable_to_consider=%cmpres(&variable_to_consider);
proc sql noprint;
  select count(*) into : obs_count from &input_dataset;
quit;
%let obs_count=&obs_count;

proc transpose data=&input_dataset out=&output_dataset prefix=top_;
    var &variable_to_consider; 
run;

data &output_dataset;
set &output_dataset end=eof;
    array top(*) top_&obs_count.-top_1;
    x=dim(top);
    call sortn(of top[*]);
    total=sum(of top[*]);

top_2_total=sum(top_1, top_2);
    if sum(top_1,top_2) > 0.9  * total then Flag90=1; else Flag90=0;
    if top_1 > total * 0.6 then Flag60=1; else Flag60=0;

keep total top_1 top_2 _name_ top_2_total total Flag60 Flag90;

run;
%mend clearance_test;


%clearance_test(input_dataset=sashelp.cars, output_dataset=work.test, variable_to_consider=mpg_city mpg_highway);

这就是确切的宏,只是使用不同的输入数据集。它在我的机器上正常工作(标志变量没有意义,因为数据不适合它们,但代码有效)。

在您同事的机器上运行相同的程序,如果它运行,那么您就知道数据是问题所在(即,数据集没有您认为的变量)。如果它没有运行,那么你还有其他问题(也许是提交方式的问题,也许你最终得到了虚假字符或其他东西)。

【讨论】:

  • 我首先在远程机器上运行了一个测试数据集,以确保一切正常(参见以前的帖子)。然后我适当地编辑了所需的代码,这就是我遇到问题的地方。我应该提一下,汇总表包含所有子集变量,但宏说它们丢失了
  • 我已将我测试过的确切 MCVE 代码添加到答案中。
猜你喜欢
  • 1970-01-01
  • 2020-06-13
  • 1970-01-01
  • 2019-07-10
  • 1970-01-01
  • 2021-07-01
  • 1970-01-01
  • 2011-01-21
  • 2012-02-02
相关资源
最近更新 更多