如何使用SAS do循环将数据输入宏?

如何使用SAS do循环将数据输入宏?,sas,Sas,我创建了以下宏和一个数据集 %censusdata (districtname=,districtnum=); data districtcodes; input distnumber distname$; cards; 1 Kaap/Cape 2 Simonstad 3 Bellville 4 Goodwood 5 Kuilsrivier 6 Wynberg run; 基本上,我想创建一个do循环,该循环从districtcodes数据

我创建了以下宏和一个数据集

%censusdata (districtname=,districtnum=);

data districtcodes;
input distnumber distname$;
cards;
1   Kaap/Cape   
2   Simonstad   
3   Bellville   
4   Goodwood    
5   Kuilsrivier 
6   Wynberg 
run;
基本上,我想创建一个do循环,该循环从
districtcodes
数据集中获取
distname
,并将其输入到
%censusdata
宏中的
DistrictName
,以及
distnumber
并将其输入到宏中的
districtnum
字段


我该怎么做呢?

正如您所发现的,数据步骤
数据线
,(又称
)与宏系统不兼容

您可能需要重新思考为什么数据必须在宏中。有一些用例,但比最初看起来要少

不管理由如何,这里有两种方法(还有更多)

  • 将数据放在数据步骤字符串中,并使用scan将其解析出来
  • 将数据放入宏变量中,并使用%scan将其解析出来
  • 在调用宏之前创建数据集,并传递数据集名称
下面是一个使用数据步骤字符串解析的示例

%macro censusdata (districtname=,districtnum=);

data districtcodes (keep=dist:);
*input distnumber distname$;
*cards;
data = "
 1   Kaap/Cape   
 2   Simonstad   
 3   Bellville   
 4   Goodwood    
 5   Kuilsrivier 
 6   Wynberg 
";


put data=;
put data= $HEX60.;

  do _n_ = 1 by 1 while (length(scan(data,_n_,' ')));
    distnumber = input ( scan (data, _n_, ' '), best8. );
    _n_ + 1;
    districtname = scan (data, _n_, ' ');
    output;

    if _n_ > 10 then stop;
  end;
  stop;
run;

%mend;

%censusdata();
您显示的宏示例似乎有点奇怪,因为您正在传递参数,表面上是为了帮助对某些数据进行操作,这些数据是宏的静态实体

更合理的方法可能是

  • 完全删除宏
  • 传递数据集的名称,以及用于某些代码生成的变量的名称
只有在满足操作模型的大量数据集上执行相同类型的处理时(即,数据集至少有两列,一列用于代码编号,第二列用于某些关联文本),这样的宏才有意义

正如您可能看到的,使用高度特定的名称和参数(例如
censusdata
districtname
districtcode
)对宏进行编码可以是一个具有很少重用价值的包装器

data districtcodes;
input distnumber distname$;
cards;
1   Kaap/Cape   
2   Simonstad   
3   Bellville   
4   Goodwood    
5   Kuilsrivier 
6   Wynberg 
run;
示例调用

%censusdata (data=districtcodes, codevar=distnumber, namevar=distname);
%mend;

假设已经开发了一个宏,可以使用call EXECUTE和数据集中的参数值来调用它

data districtcodes;
 input distnumber distname$;
 str=catt('%censusdata(districtname=', distname, ' , districtnum=', distnumber, 
    ');');
 call execute (str);
 cards;
1   Kaap/Cape   
2   Simonstad   
3   Bellville   
4   Goodwood    
5   Kuilsrivier 
6   Wynberg
;
 ;
 ;; 
run;

你的意思是
%macro-censusdata(districtname=,districtnum=)?例如,您是否缺少%macro部分?您正在查找
CALL EXECUTE()
DOSUBL