Error while executing a data step

Occasional Contributor
Posts: 13

Error while executing a data step

[ Edited ]
options msglevel=i;
libname in1 "/sascommon/sasusers/ksnm843";
%let subdir=/sascommon/gridnode01/config/Lev1/SASApp94/WorkspaceServer/RTRACE_LOGS;
data work.dirlist(keep=filename strUser ModDT CrDT strPID strSource);
/* must be explicitly set to blank to have the filename function generate a fileref */
fileref = '      ';
rc = filename(fileref,"&subdir");
/* open fileref and loop throughfilenames */
do i = 1 to dnum(did);
/* extract filename */
fileName = dread(did,i);
/* find the position of PID in the filename */
pid_start = index(fileName,"PID")+3;
/* find the end position of PIF in filename */
pid_end = index(fileName,"DATE")-1;
/* extract PID */
strPID = substr(fileName,pid_start,pid_end - pid_start);
/* find the start of DATE in filename */
date_start = index(fileName,"DATE")+4;
/* find the end position of DATE in the filename */
date_end = index(fileName,":")-1;
/* set the start of the time field */
time_start = date_end +2;
/* find the end of the time field */
time_end = index(filename,".");
/* extract creation time */
tmpTime = Trim(substr(filename, time_start, time_end - time_start));
tmpHour = Substr(tmpTime,1, 2)*1;
tmpMin = Substr(tmpTime,3,2)*1;
tmpSec = Substr(tmpTime,5,2)*1;
/* extract creation date */
strDT = trim(substr(fileName,date_start,date_end - (date_start-1)));
tmpDT = input(put(strDT,8.),yymmdd8.);
CrDt = DHMS(put(tmpDT,8.),tmpHour,tmpMin,tmpSec);
fid = mopen(did,fileName);
if fid then do;
/* extract userid */
strUser=finfo(fid, foptname(fid, 2));
/* extract file modified date */
ModDT=input(trim(finfo(fid, foptname(fid, 5))),datetime20.);
format ModDT CrDT datetime20.;
/* extract file location */
strSource = finfo(fid, foptname(fid, 1));
rc = dclose(did);
rc = filename(fileref);

I got the following error when I executed the above mentioned program


ERROR: Invalid DO loop control information, either the INITIAL or TO expression is missing or the BY expression is missing, zero,
or invalid.


I have also attached the detailed log


Please help me resolve this error

Valued Guide
Posts: 580

Re: Error while executing a data step

Please post Code and Log using {i} or running-man icon. To debug errors the log is almost always required. Check Variable did, maybe dopen failed.

Super User
Posts: 10,278

Re: Error while executing a data step

When code fails, use put statements to show the state of variables during execution. Doing this, I discovered that your dopen() call returns zero, which points to an invalid fileref. This happens because you did not use a valid name (an empty string is no name) for your fileref.

Do this:

data work.dirlist (keep=filename strUser ModDT CrDT strPID strSource);
fileref = 'mydir';

and the rest of the step will work somewhat. You'll get lots of messages because you treat numeric variables/values incorrectly (using substr() directly on a numeric variable usually returns bogus).

Make sure to properly convert numbers to strings, or use the proper functions to retrieve hours/minutes/seconds from time values.

Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Occasional Contributor
Posts: 13

Re: Error while executing a data step

Posted in reply to KurtBremser

Thank you so much ! Smiley Happy This worked !!

Respected Advisor
Posts: 4,736

Re: Error while executing a data step

As @KurtBremser already indicates the main reason that you're code is failing is due to below statement:

fileref = ' ';

This creates your variable fileref (give it another name) with a length of 1 character which is too short to hold a fileref which can be up to 8 characters.

Add the following and the syntax error will go away (unlike all the warnings caused by other less than perfect code bits):

length fileref $8;
call missing(fileref);
Ask a Question
Discussion stats
  • 4 replies
  • 1 like
  • 4 in conversation