我目前有一个SAS流程可以生成多个数据集(无论是否有观察结果)。我想确定一种基于观察总数控制导出过程的方法(如果nobs> 0,则导出)。我的第一次尝试是原始的,使用if / then逻辑比较select into
宏var(在数据集中计算obs) -
DATA _NULL_;
SET A_EXISTS_ON_B;
IF &A_E > 0 THEN DO;
FILE "C:\Users\ME\Desktop\WORKLIST_T &PDAY..xls";
PUT TASK;
END;
RUN;
这里的问题是我没有办法将多个集合写入具有多个工作表的同一工作簿(或者我?)
此外,每当我尝试添加另一个具有类似逻辑的“Do”块时,执行失败。如果使用数据 null 无法做到这一点,那么ODS会成为答案吗?
答案 0 :(得分:0)
建议您使用不同的方法来编写XLS文件。
关于使用DATA步骤或PROC步骤,DATA步骤可以容忍具有0 obs的数据集。如果SET语句读取的数据集为0 obs,则只会结束该步骤。所以你不需要特殊的逻辑。大多数PROCS还可以容纳0个数据集,而不会发出警告或错误。
例如:
1218 *Make a 0 obs dataset;
1219 data empty;
1220 x=1;
1221 stop;
1222 run;
NOTE: The data set WORK.EMPTY has 0 observations and 1 variables.
1223
1224 data want;
1225 put "I run before SET statement.";
1226 set empty;
1227 put "I do not run after SET statement.";
1228 run;
I run before SET statement.
NOTE: There were 0 observations read from the data set WORK.EMPTY.
NOTE: The data set WORK.WANT has 0 observations and 1 variables.
1229
1230 proc print data=empty;
1231 run;
NOTE: No observations in data set WORK.EMPTY.
但请注意,Joe指出,PROC EXPORT将很乐意导出一个0 obs的数据集并写入一个包含0条记录的文件,如果已存在则覆盖。 e.g:
1582 proc export data=sashelp.class outfile="d:\junk\class.xls";
1583 run;
NOTE: File "d:\junk\class.xls" will be created if the export process succeeds.
NOTE: "CLASS" range/sheet was successfully created.
1584
1585 data class;
1586 stop;
1587 set sashelp.class;
1588 run;
NOTE: The data set WORK.CLASS has 0 observations and 5 variables.
1589
1590 *This will replace class.xls";
1591 proc export data=class outfile="d:\junk\class.xls" replace;
1592 run;
NOTE: "CLASS" range/sheet was successfully created.
ODS声明可能也会这样做。
我使用宏来检查数据集是否为空。所以回答如下: How to detect how many observations in a dataset (or if it is empty), in SAS?