SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
Sangavi
Obsidian | Level 7
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 */
did=dopen(fileref);
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);
infonum=foptnum(fid);
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));
output;
end;
end;
rc = dclose(did);
rc = filename(fileref);
run;

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

4 REPLIES 4
andreas_lds
Jade | Level 19

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.

Kurt_Bremser
Super User

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.

Sangavi
Obsidian | Level 7

Thank you so much ! Smiley Happy This worked !!

Patrick
Opal | Level 21

As @Kurt_Bremser 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);

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1154 views
  • 1 like
  • 4 in conversation