宏错误:找到比定义的更多位置参数

时间:2018-05-22 06:17:20

标签: macros sas

示例数据:

Reportstatestatus age male hospid radm30 uniquekey Timemonths clinical_cond1 clinical_cond2  
ca1                33  0    1232   1      1           13       0              1
ca1                22  1    2321   1      2           10       1              0
ca0               22   1    2222   0      3           10       1              0

我有一个像这样的宏(由其他人设计的代码):

 DSN = Dataset Name 
PARMS = Parameters from multivariate model 
KEY = Unique ID or key to match observations 
DEPENDENT = Dependent variable specified in model 
INDEPENDENT = Independent variables separated with spaces 
CLASS = Class variable of interest to calculate marginal effects 
NAME_1 = User provided name for the first data element of class variable 
(Name limited to 30 characters) 
NAME_2 = User provided name for the second data element of class variable 
(Name limited to 30 characters) 
*************************************************************** 
EXAMPLE OF MACRO CALL 
***************************************************************/ 
%MARGINAL(cabg, /* Name of original dataset */ 
parms_OpDeath, /* Parameter estimates from model */ 
medRecN, /* Key */ 
yom, /* Dependent variable */ 
age_n, /* Independent variables listed */ 
GENDER, /* Class variable for marginal effects */ 
Male, /* User-supplied name of first data element*/ 
Female); /* User-supplied name of second data element*/ 
/****************************************************************/ 


%MACRO MARGINAL (DSN,PARMS,KEY,DEPENDENT,INDEPENDENT,CLASS,NAME_1,NAME_2);  
data OUT_1;  
length &CLASS $30
;  
set &DSN (rename=(&CLASS = OLD_CLASS )) ;  
&CLASS = "&NAME_1";  
RUN;  
**** Apply model to entire data; 
PROC LOGISTIC inmodel= &PARMS Descending;  
score data=OUT_1  
OUT= PRED_1 (KEEP = &KEY &DEPENDENT &CLASS &INDEPENDENT OLD_CLASS P_1);  
title "Applying the Risk Model to Entire Data Assuming Every Observation’s 
Class Variable Value = First Data Element of the Class Variable";  
RUN;  
data OUT_2;  
length &CLASS $
30
;  
set &DSN (rename=(&CLASS = OLD_CLASS )) ;  
&CLASS = "&NAME_2";  
RUN;  
**** Apply model to entire data; 
PROC LOGISTIC inmodel= &PARMS Descending;  
score data=OUT_2  
OUT= PRED_2 (KEEP = &KEY &DEPENDENT &CLASS &INDEPENDENT OLD_CLASS P_1);  
title1 "Applying the Risk Model to Data assuming Every Observation’s Class 
Variable Value = Second Data Element of the Class Variable";  
RUN;  
**** Join the data; 
data OUT_1_N;  
set PRED_1;  
p_&NAME_1 = p_1;  
KEEP &KEY &DEPENDENT &CLASS &INDEPENDENT OLD_CLASS p_&NAME_1 ;  
RUN;  
proc sort data=OUT_1_N; by &KEY; RUN;  
data OUT_2_N;  
set PRED_2;  
p_&NAME_2 = p_1;  
KEEP &KEY &DEPENDENT &CLASS &INDEPENDENT OLD_CLASS p_&NAME_2 ;  
RUN;  
proc sort data=OUT_2_N; by &KEY; run;  
data join;  
merge OUT_1_N OUT_2_N;  
by &KEY;  
RUN;  
title "Compare Observed Response Variable: Class Variable Value 1 vs. Class 
Variable Value 2";  
PROC TTEST data=join;  
class OLD_CLASS;  
var &DEPENDENT;  
RUN;  
title "Compare predicted Response Variable with Recycle Prediction Method";  
PROC MEANS data=join n mean std min max lclm uclm;  
var p_&NAME_1 p_&NAME_2 ;  
RUN;  
PROC TTEST data=join;  
paired p_&NAME_1 * p_&NAME_2;  
RUN;  
%MEND MARGINAL;

当我像这样运行宏时:

libname inputf "F:\ccorp\mray\processed\Readmission";
data work.readmdata;
set INPUTF.readm_caonly;  
run;

%LET indep = age Male TimeMonths Reportstatestatus Hx_CTSURG 
Cardiogenic_Shock COPD MCANCER DIABETES MALNUTRITION DIS_FLUID OBESITY 
HEMATOLOGICAL DEMENTIA MAJOR_PSYCH PARALYSIS_FUNCTDIS POLYNEUROPATHY 
CHF ARRHYTHMIAS CEREB_HEMORR CEREB_DISEASE VASDIS_WCOMP FIBROSIS_LUNG 
PNEUMONIA OTHER_LUNG_DIS DIALYSIS RENALFAILURE DECUBITUS_ULCER ;

(Reportstatestatus之后的变量是临床条件)

%MARGINAL (%nrbquote(readmdata, /* Name of original dataset */ 
estimates_slopeCA, /* Parameter estimates from model */ 
UniqueKey, /* Key */ 
RADM30, /* Dependent variable */ 
indep, /* Independent variables listed */ 
Reportstatestatus, /* Class variable for marginal effects */ 
CA1, /* User-supplied name of first data element*/ 
CA0)); /* User-supplied name of second data element*/ 

它给了我错误:

  

错误:找到的位置参数多于定义的位置参数。

我已经在线研究并尝试过%STR或%NRSTR,%BQUOTE或%SUPERQ(这没有运行。它提供了更多错误)。 我仍然得到错误。

请帮忙。

3 个答案:

答案 0 :(得分:0)

如果宏调用本身存在问题,则不需要数据(甚至所有宏代码)。那就是说,我无法复制你的问题。

要调试,我会创建一个简单的宏,如:

%MACRO MARGINAL (DSN,PARMS,KEY,DEPENDENT,INDEPENDENT,CLASS,NAME_1,NAME_2);  
  %put _user_ ; 
%MEND MARGINAL;

我用您的代码成功调用了该宏:

%MARGINAL (%nrbquote(readmdata, /* Name of original dataset */ 
estimates_slopeCA, /* Parameter estimates from model */ 
UniqueKey, /* Key */ 
RADM30, /* Dependent variable */ 
indep, /* Independent variables listed */ 
Reportstatestatus, /* Class variable for marginal effects */ 
CA1, /* User-supplied name of first data element*/ 
CA0)); /* User-supplied name of second data element*/ 

通过“成功”我的意思是它执行了宏,并没有产生错误报告。由于使用了%NRBQUOTE(),因此只为DSN参数赋值。这可能不是预期的行为,但它不会产生错误。即使我删除%NRBQUOTE,我也不会收到错误。

我建议您编辑问题以制作可重现的示例。

答案 1 :(得分:0)

我会将函数放在代码中而不是宏输入参数中。 然后在你的使用中强制引用它... 如果你给我全部的宏观辩护,我可以帮忙!

答案 2 :(得分:0)

只有最后两个参数可能需要应用宏引用才能进行调用。这两个参数需要字符串来匹配指定类变量的可能值。即使这样,只有当值包含逗号,前导空格,不平衡的引号或不平衡的括号时,您才需要添加宏引用。

其他参数都需要数据集或变量名称或其他有效的SAS语法,因此不需要添加宏引用。

cust_id<-sample(1:20,size = 100,replace = T)
prod_id<-sample(1:10,size=100,replace = T)
prod_id<-paste0("prodid",prod_id)
atm<-sample(1:10,size=100,replace = T)*100
atm[(sample(1:100,size=30,replace =F ))]<-0
sales<-data.frame(cust_id,prod_id,atm)  
sales