You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SAS运行mcartest宏时为何查找格式?格式位置在哪?

SAS宏运行时格式查找错误及格式库创建问题

我修改了mcartest宏以支持输入参数,宏代码开头如下:

macro mcartest (data=,items=);
/* PROVIDE VALUES FOR THE FOLLOWING MACRO VARIABLES */
%let datafile = &data; * dataset to be analyzed;
%let numvars = %sysfunc(countw(&items));
%let misscode = .; * SPECIFY MISSING VALUE CODE;
%let varnames = &items; * SPECIFY VARIABLE NAMES;
%let seednum = 564321; * SPECIFY RANDOM SEED;
/* DO NOT ALTER THE CODE BELOW */
data one;
set &datafile ;

array m[&numvars] &varnames ; array r[&numvars] r1 - r&numvars ;
do i = 1 to &numvars;
if m[i] = &misscode then m[i] = .;
end; drop i;
do i = 1 to &numvars;
r[i] = 1;
if m[i] = . then r[i] = 0;
end; drop i;

proc sort;
by r1-r&numvars;

...

运行宏时,SAS创建数据集ONE的过程中出现大量格式相关错误,日志片段如下:

MPRINT(MCARTEST):   data one;
MPRINT(MCARTEST):   set nicole.rawdatarecoded ;
MPRINT(MCARTEST):   array m[74] LASSI1 LASSI2 LASSI3 LASSI4 LASSI5 LASSI6 LASSI7 LASSI8 LASSI9
LASSI10 LASSI11 LASSI12 LASSI13 LASSI14 LASSI15 LASSI16 LASSI17 LASSI18 LASSI19 LASSI20 LASSI21
LASSI22 LASSI23 LASSI24 LASSI25 LASSI26 LASSI27 LASSI28 LASSI29 LASSI30 LASSI31 LASSI32 LASSI33
LASSI34 LASSI35 LASSI36 SWLS_1 SWLS_2 SWLS_3 SWLS_4 SWLS_5 ISEL1 ISEL2 ISEL3 ISEL4 ISEL5 ISEL6
ISEL7 ISEL8 ISEL9 ISEL10 ISEL11 ISEL12 DASS1 DASS2 DASS3 DASS4 DASS5 DASS6 DASS7 DASS8 DASS9
DASS10 DASS11 DASS12 DASS13 DASS14 DASS15 DASS16 DASS17 DASS18 DASS19 DASS20 DASS21 ;
MPRINT(MCARTEST):   array r[74] r1 - r74 ;
MPRINT(MCARTEST):   do i = 1 to 74;
MPRINT(MCARTEST):   if m[i] = . then m[i] = .;
MPRINT(MCARTEST):   end;
MPRINT(MCARTEST):   drop i;
MPRINT(MCARTEST):   do i = 1 to 74;
MPRINT(MCARTEST):   r[i] = 1;
MPRINT(MCARTEST):   if m[i] = . then r[i] = 0;
MPRINT(MCARTEST):   end;
MPRINT(MCARTEST):   drop i;

ERROR: Width specified for format F is invalid.
NOTE: Format STATUSA was not found or could not be loaded.
ERROR: Width specified for format F is invalid.
ERROR: Width specified for format F is invalid.
NOTE: Format FINISHEA was not found or could not be loaded.
NOTE: Format CONSENTA was not found or could not be loaded.
NOTE: Format AGEA was not found or could not be loaded.
NOTE: Format YEAR_INA was not found or could not be loaded.
NOTE: Format RACE_ETA was not found or could not be loaded.
NOTE: Format GENDER_A was not found or could not be loaded.
NOTE: Format ISEL12_A was not found or could not be loaded.
NOTE: Format ISEL121A was not found or could not be loaded.
NOTE: Format ISEL122A was not found or could not be loaded.
NOTE: Format ISEL123A was not found or could not be loaded.
NOTE: Format ISEL124A was not found or could not be loaded.
NOTE: Format ISEL125A was not found or could not be loaded.
NOTE: Format ISEL126A was not found or could not be loaded.
*and so on for all the variables.*

现咨询:SAS为何会查找这些格式?这些格式所在的格式库何时创建?


解答

1. SAS查找格式的原因

  • 你的源数据集nicole.rawdatarecoded中的变量预先绑定了用户自定义格式(如日志中的STATUSA、FINISHEA等)。SAS读取数据集时,会自动尝试加载变量关联的格式定义,若找不到对应格式或存储格式的库,就会抛出提示或错误。
  • 日志中的ERROR: Width specified for format F is invalid,说明某个变量绑定的F格式存在定义问题,比如格式宽度参数设置错误,或者该格式本身未被正确定义。

2. 格式库的创建时机

用户自定义格式通过PROC FORMAT程序创建,存储位置分为两种:

  • 临时格式库:默认存储在WORK.FORMATS,仅在当前SAS会话有效,会话结束后自动删除。创建时无需指定LIBRARY参数:
    proc format;
      value STATUSA 1='已完成' 0='未完成';
      /* 其他格式定义 */
    run;
    
  • 永久格式库:需先通过LIBNAME语句定义库路径,再在PROC FORMAT中用LIBRARY=参数指定存储位置,示例:
    libname myfmt "D:/sas_formats";
    proc format library=myfmt;
      value FINISHEA 1='是' 0='否';
      /* 其他格式定义 */
    run;
    
  • 要让SAS能找到这些格式,需在运行宏前,用OPTIONS FMTSEARCH=(WORK myfmt);指定格式搜索路径,把包含目标格式的库加入搜索列表。

内容的提问来源于stack exchange,提问作者Steve Scher

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 07:35:57