如何通过批处理从SAS代码打开SAS文件?

时间:2019-02-05 16:48:26

标签: batch-file sas sas-macro

我有一个SAS宏程序,可以测试打开文件并在日志中输出观测值的总数。通过SAS运行它时,我能够在SAS日志中输出观测值的数量(按预期)。但是,当我通过批处理脚本运行SAS程序时,未检测到文件/无法打开该文件。我在做什么错?

请参阅SAS程序的代码:

%macro syserrClass;

options symbolgen;

/* Define macro variables */
%let usernam = %sysget(pUserNam);
%let evSuffix = %sysget(pSuffix); /* Suffix of program name: eg. year 2018 */
%let evSeq = %sysget(pSeq);
%let evLib01 = %sysget(pLib01); /* SAS lib name for SAS data set */

*%let usernam = myusername;
*%let evSuffix = 2014;
*%let evSeq = 02;
*%let evLib01 = mor2014; /* SAS lib name for SAS data set */

%let filen03 = ije_mortality_&evSuffix.&evSeq._err; /* Input file */

LIBNAME &evLib01. v9 "C:\Users\&usernam.\Documents\My SAS Files\9.4\&evLib01.";

/* Open data set */
%let dsId1 = %sysfunc(open(&evLib01..&filen03.));

/* If the data set exists, get the number of observations */
%if &dsId1. %then %do;
 %let nobs1 = %sysfunc(attrn(&dsId1.,nobs));
 %let rc = %sysfunc(close(&dsId1.));
 %put &evLib01..&filen03. has &nobs1. observation(s).;
%end;
%else
%put Open for data set &evLib01..&filen03. failed - %sysfunc(sysmsg());

%mend;

/* Call macro */
%syserrClass;

请参阅批处理脚本(.cmd)的代码:

@echo off

setlocal

set "pUserNam=%username%"
set "pSuffix=%1"
set "pSeq=%2"
set "pLib01=%3"

set inPath01="C:\Users\%pUserNam%\Documents"
set inPath02="My SAS Files\9.4\"

set pInPath03=C:\Users\%pUserNam%\Documents\log
set pInPath05="C:\Program Files\SASHome\SASFoundation\9.4\sas.exe"
set pInPath06="C:\Program Files\SASHome\x86\SASFoundation\9.4\sas.exe"

rem set SAS exe path depending on existence of path
if exist %pInPath05% (set programname=%pInPath05%) else (set programname=%pInPath06%)

rem folder with output files - sas
set pLib02=mor%pSuffix%

rem Print summary of exceptions grouped by file name
set sasprogram="C:\Users\%username%\Documents\sascode\test_exceptrpt_macro01.sas"
%programname% -sysin %sasprogram% -log %pInPath03%\ije_mortality_exceptrpt01_%pSuffix%%pSeq%.txt -set pUserNam %pUserNam% -set pSuffix %pSuffix% -set pSeq %pSeq% -set pLib01 %pLib02%
echo SAS Program: %sasprogram%
if %ERRORLEVEL% GTR 1 (
 echo ERROR: Return code = %ERRORLEVEL%
 goto endproc
  ) else (echo NOTE: Return code = %ERRORLEVEL%)

:endproc

在运行批处理时,在日志中突出显示的主要输出是: SYMBOLGEN: Macro variable DSID1 resolves to 0 SYMBOLGEN: Macro variable EVLIB01 resolves to mor2014 SYMBOLGEN: Macro variable FILEN03 resolves to ije_mortality_201402_err Open for data set mor2014.ije_mortality_201402_err failed - ERROR: Libref MOR2014 is not assigned.

但是,当我切换SAS代码以使用SAS交互式变量时(请参阅我如何评论%letusernamevSuffix的{​​{1}}的第二组,和evSeq),我在日志中得到了这个: evLib01

1 个答案:

答案 0 :(得分:1)

打开MPRINT,以便您可以看到LIBNAME语句的全文。这将帮助您解决错误。

Open for data set mor2014.ije_mortality_201402_err failed - ERROR: Libref MOR2014 is not assigned.