0
votes

I have a SAS macro program to test opening a file and outputting the total number of observations in the log. When I run it through SAS, I am able to output the number of observations in the SAS log (as intended). However, when I run the SAS program through a batch script, the file is not detected/can not be opened. What am I doing incorrectly?

See code for SAS program:

%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;

See code for batch script (.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

The main output that stands out in the log when I run through batch is: 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.

However, when I switch up the SAS code to use the SAS interactive variables (see how I commented the second group of %let of usernam, evSuffix, evSeq, and evLib01), I get this in the log: SYMBOLGEN: Macro variable DSID1 resolves to 2 SYMBOLGEN: Macro variable EVLIB01 resolves to mor2014 SYMBOLGEN: Macro variable FILEN03 resolves to ije_mortality_201402_err SYMBOLGEN: Macro variable NOBS1 resolves to 5 mor2014.ije_mortality_201402_err has 5 observation(s).

1

1 Answers

1
votes

Turn on MPRINT so you can see the full text of the LIBNAME statement. That will help you resolve the error.

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