Steps to implementing a scan loop in SAS
Steps:
1. Store the number of observations in a data set to a macro variable (RECCOUNT)
2. Increment a variable (I) fromone to RECOUNT using a %DO loop
3. Use the FIRSTOBS option in a DATA step to advance to the record I
4. Store record values to macro variables using CALL SYMPUT
5. Perform any desired PROC and DATA steps
EXAMPLE:
In the following example, a log file (DATALOG) has been created of all the data sets in a library named TEST. Periodically, it is necessary to print the contents of these data sets into a series of quick reports. DATALOG consists of two columns, FILENM (the file name) and DESC (a short description). The reports should be formatted with a standard PROC PRINT and have a title consisting of the table description. The following piece of code accomplishes that task.
/* Macro to SCAN through DATALOG */ %macro scanloop(scanfile,field1,field2); /* First obtain the number of */ /* records in DATALOG */ data _null_; if 0 then set &scanfile nobs=x; call symput("reccount",x); stop; run; /* loop from one to number of records */ %do i=1 %to &reccount; /* Advance to the Ith record */ data _null_; set &scanfile(firstobs=&i); /* store the variables of interest in macro variables */ call symput('var1',&field1); call symput('var2',&field2); stop; run; /* now perform the tasks that wish repeated for each observation */ proc print data=&var1; title "&var2"; %end; %mend scanloop; /* Call SCANLOOP */ %SCANLOOP(DATALOG,FILENM,DESC); RUN;