DATA Step, Macro, Functions and more

Question on a macro program meaning

Reply
Occasional Contributor
Posts: 14

Question on a macro program meaning

Hi All,

I came across a macro, and I believe I got the basic understanding of the program, but would like some confirmation.

datalogread;

length file $256 dye 8 dte1 8 directory $256;

format dte datetime;

keep file dye dte1 directory;

length filrf filerfb $8;

directory =‘&directory’;

rc1=filename(filrf,directory);

opdir=dopen(filrf);

memcount=dnum(opdir);

if opdir <=0 then put ‘ERROR: Unable to open log folder.’;

do i=1 to memcount;

file=dread(opdir,i);

rc2=filename(filrfb,catx(‘\’,directory,file));

fid=fopen(filrfb);

dte=input (finfo(fid,’Create Time’),datetime.);

dte1=dte;

if dye >=&lastrun then output;

rc3=fclose(fid)

rc4=filename(filrfb);

end;

rc5=dclose(opdir);

run;

Would appreciate another pair of eyes, to help me understand this program.

Your help on this is greatly appreciated.

Thanks You.

Super User
Posts: 19,767

Re: Question on a macro program meaning

What do you think it does? And what don't you understand?

Best way to understand code when learning is to comment every line.

Occasional Contributor
Posts: 14

Re: Question on a macro program meaning

It looks like the program is indicating to go to a specified directory, check the files (possibly log files) and check the files for errors.

SAS Super FREQ
Posts: 8,861

Re: Question on a macro program meaning

Hi, ... to add onto the previous comments...

  And, the only possible reference to anything to do with SAS macro processing is this statement:

directory ='&directory';

which may or may not be a reference to a SAS macro variable. Do you know whether &directory is a SAS macro variable? And if so, does this program even work? Can you find a statement where the value of &directory is set (such as a %LET statement or a CALL SYMPUT)? One thing that you could find out about referencing SAS Macro variables is that they will not resolve correctly unless they are enclosed in double (") quotes. So unless you are creating a literal string of '&directory' as the value for your directory data step variable, my suspicion is that your program is not working as you expect.

In addition, this may be problematic:

datalogread;

because usually the keyword DATA is separated from the dataset name by a space. So your code could be:

DATA logread; or DATA log read; depending on whether you were creating 1 dataset in the WORK library or 2. Or, I suppose that LOG could be your LIBRARY and this is a possibility: DATA LOG.READ; -- it really depends on what you expect to be the output of the program.

cynthia

Super User
Posts: 7,757

Re: Question on a macro program meaning

Actually this is not a macro at all, just a datastep that looks for files in given directory that were created after a given date, using the SAS file and directory I/O functions.

The directory name and the date are obviously specified somewhere farther "up" in the code.

To understand this, look at the documentation of the functions involved.

And there are a lot of syntax and semantic errors.

- datalogread should be data logread

- the format statement will not work, as the format spec is missing the dot

- dye is in the keep list and in a condition, but never has a value assigned (is always missing)

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Ask a Question
Discussion stats
  • 4 replies
  • 276 views
  • 0 likes
  • 4 in conversation