【发布时间】:2017-12-28 10:52:14
【问题描述】:
我正在尝试使用宏中的循环对 SAS 中的多个数据集进行排序,使用数据集名称中的数字列表(其中一些具有前导零)(例如,在示例代码中,01,02 的列表),并且此代码还将指导我想构建的其他一些循环。
我使用 SAS 指南来循环使用宏 DO 循环代码作为起点的非顺序值列表:http://support.sas.com/kb/26/155.html。
data dir1201;
input compid directorid X;
format ;
datalines;
01 12 11
02 15 5
;
run;
data dir1202;
input compid directorid X;
format ;
datalines;
01 12 1
03 18 8
;
run;
%macro loops1(values);
/* Count the number of values in the string */
%let count=%sysfunc(countw(&values));
/* Loop through the total number of values */
%do i = 1 %to &count;
%let value=%qscan(&values,&i,%str(,));
proc sort data=dir12&value out=dir12sorted&value nodupkey;
by directorid compid;
run;
%end;
%mend;
options mprint mlogic;
%loops1(%str(01,02))
我假设非顺序列表需要str,但当我想保留前导零时这也很有用;
我看到宏变量似乎在日志中包含 01 或 02,但随后我立即收到错误 22 和 200。这是使用此示例代码的日志错误的 sn-p:
339 %macro loops1(values);
340 /* Count the number of values in the string */
341 %let count=%sysfunc(countw(&values));
342 /* Loop through the total number of values */
343 %do i = 1 %to &count;
344 %let value=%qscan(&values,&i,%str(,));
345 proc sort data=dir12&value out=dir12sorted&value nodupkey;
346 by directorid compid;
347 run;
348 %end;
349 %mend;
350 options mprint mlogic;
351 %loops1(%str(01,02))
MLOGIC(LOOPS1): Beginning execution.
MLOGIC(LOOPS1): Parameter VALUES has value 0102
MLOGIC(LOOPS1): %LET (variable name is COUNT)
MLOGIC(LOOPS1): %DO loop beginning; index variable I; start value is 1; stop value is 2; by
value is 1.
MLOGIC(LOOPS1): %LET (variable name is VALUE)
NOTE: Line generated by the macro variable "VALUE".
1 dir1201
--
22
--
200
ERROR: File WORK.DIR12.DATA does not exist.
我不明白为什么会显示dir1201,但是错误是引用数据集work.dir12(忽略01)
【问题讨论】:
-
您熟悉 CALL EXECUTE 吗?我觉得它在这里可能会更好。我也想知道你是否在这里循环日期,如果你是,那么这个例子可能会有所帮助。 documentation.sas.com/…
-
我见过 CALL EXECUTE,但我不记得使用过它或阅读过很多关于它的内容。我会调查的。我正在遍历代表一年数据的数据集,但我没有看到在 SAS 中使用两位数独立年份格式。我认为以下答案可以让我使用我所拥有的。