/* Dexter: program logic overview. John Blodgett Rev. 5/14/2005 10:55AM 1. We begin (above) with global declarations of variables, most of which get passed to the program as parms. We expect that these will originate as user choices off a dynamically generated menu page which is created by companion program uex2dex.sas, which gets invoked from the uexplore application. This section of the program concludes with 3 invocations of our mvparms utility which insures that our multi-valued select list parms idv, v and table always have values for idv0 and idv1, v0 and v1, table0 and table1 - even if less then 2 of these are chosen. The critical %include '/pub/sascode/autoexec.web.sas'; statement accesses the autoexec module has to be included so that the module is able to resolve references to utility macros and to stored SAS library formats. NOTE that the dexter macro appears at the very end of the program, and is comprised almost entirely of macro invocation statements. The macros as they appear in the source and as we describe them here are invoked by dexter in that order, i.e. first edtparms, then dirinit, then metaset, etc. However, no macros are actually invoked until the very last line of the module - the invocation of dexter - does anything get executed. At that point the macros (which have been scanned and compiled as they are read by SAS and presumably have passed the first hurdle of not having any macro syntax problems) are actually executed which, of course, generates SAS code which is passed to the SAS supervisor for execution. To summarize, we have the following code structure for the websas/dexter.sas module: -Housekeeping statements, comments and global parm declarations. The %mvparms macro is invoked for each of the possible multi-valued select parms. -Macro definition code, in the order they will be invoked, but without any immediate invocations. -Last macro defined is %dexter, which is just a "driver" macro, which invokes all the others. -Last line of code is the invocation of %dexter, which causes everything to start happening. (while debugging, note that errors in the code can easily cause the dexter invocation line to be "lost", e.g. it could become part of a comment because of a missing closing quote or parenthesis somewhere, and thus nothing really happens. This should never happend with the production version, of course, since presumably all such errors will be fixed before the module becomes production. 2. The edtparms macro handles various parm checking and editing tasks. Errors in the specs are handled (as are all errors here) by assigning a value to errmsg and letting the ckforerr library macro display the message back to the browser window. edtparms assigns values to global parms such as sortvars, sortby_stmt, nfkeys, filtvars, libref, setref, set, keepvars1, keepvars2, and query_str. Notice that when a query parm is specified the qryflg global parm gets set to true and a lot of code gets by-passed because we assume the parms in a stored query have already been verified. 3. The dirinit macro handles the work of determining how many of these extract jobs have run on the current day so it can assign a number to this job and use it to create (via Unix "mkdir") and cd to a new temporary directory in /tmp/scratch. (If a global parm, jobid, is passed then the program checks for the existence of a directory named for that jobid and points to it, reusing the old directory. This requires somebody who really knows what they're doing adding an extra parm spec.) This macro generates the sasout libref and the summary fileref. It also generates a short data _null_ step that writes header stuff to the browser (_webout). (This step will be bypassed if nomenu=1 parm is specified.) The last thing it writes (pipes) to the browser/user is the "....extraction work in progress.... Please be patient." message. The sasout libname includes an "outrep=windows_32" spec when SAS version 9 is detected. This was needed so that windows SAS users can access the output SAS dataset if they choose that as 1 of their output formats. (In Version 8 this was not necessary, but with the conversion to 64-bit format in v9 SAS datasets on Unix and Windows are not compatible.) 4. The metaset macro creates work._contents with metadata describing the input dataset. The set is indexed by an upper-cased variable name to allow keyed access in subsequent steps. 5. The bldquery macro does the work of processing the filter logic parms (the 5 rows of vars, ops and values, along with paren flags and connecting logical operators). It uses a data step to do the creation of a single "where" variable which is then symput-ted to create the critical &where global variable. 6. The genext macro does the actual extracting, i.e. it reads the input dataset and creates sasout.xtract (or work.aggin, a view, if aggregation is specified) , making use of the where filter created in bldquery and the variable lists from edtparms. It includes a sort step if user specified sorting. Each of the following output-generating modules relies on being able to access the xtract dataset. 6.a If aggregation is specified then the aggext macro will sort and aggregate the aggin data and store the aggregated output in sasout.xtract. (added 5/05). -----------Output Generating Modules--------------- Each of these modules accesses the sasout.xtract dataset created by genext (or aggext) to create an output file of some type (report, delimited file or database/spreadsheet). In each case the module writes to the summary log file and also (normally) to _webout. The _webout output provides a very brief description and a hyperlink to the generated output. That generated output is stored as a file in the job directory as created in dirinit. An exception to this general rule is if the user checks the button at the end of Section I on the form saying they want the ouput piped directly to their browser. In this case the output is not written to a file, but rather directly to _webout (the browser window). This only applies when the user has selected only 1 output of a type that can be directly generated. 7. The genrpt macro will generate a report in html, pdf or plain text format as (and if) specified. 8. The gendlm macro generates the delimited output file as (and if) specified. Where once we used our own %cnvtdlm macro to generate these files, we have converted over to the SAS export proc to create the output starting with version 8 of the SAS software. Unless the user asks for their output to be generated directly to the browser we insert an extra step that reads the sas-generated file and creates a final version that includes an extra header row with variable labels, written as the 2nd line of the file, following the first line which has variable names. The lrecl of the delimited file is specified as 8192, which might be too small if the extract is too large. Delimiter characters are removed from the labels (commas or tabs). 9. The gendbms macro generates the database output file (if requested) and writes lines to _webout and the summary file. If the output dbms is SAS then it simply points to the exisiting xtract.sas7bdat file (dataset). If it is a dbf file then proc export is used to generate the xtract.dbf file. The code currently seems to indicate that we can also generate mdb and xls output, but this is not supported on the Unix platform. We leave it there in case some day SAS I fixes this. 10. The savequery macro writes the query.txt file in the current directory. This file encapsulates the user specs used to generate the output. For now (5/05) this is mostly of use to internal personnel who can capture this code and store it in a permanent Queries directory within the data directory for use as a saved query. We hope at some point to create a method whereby users could contribute their own saved queries, complete with parm modifications. Long story. Another logic module. 11. The wrapup macro closes out the output piped back to the user and the summary file. It also checks for a zapdir parm which will cause it to delete the temp directory associated with the run. (This is only to be used with web-based runs where we have pages that refer to these apps and should only be used in conjuction with the nomenu=1 option, since we cannot delete the results until the user has finished viewing and/or download them.) 12. The final macro is the dexter driver module. It's relatively short, since mostly it just invokes the other macros. It also invokes a macro ckforerr in several places. This is a utility macro that is included from /pub/sasmacro/ckforerr.sas. It checks to see if the global parm errmsg has a non-blank value. If it does the macro writes the contents of that parm to _webout. In most cases we follow the invocations of this macro with the statement %if %superq(errmsg) ne &null %then %goto endjob; which says the game is over - go to the end of the dexter macro, thus skipping over the otherwise invoked processing modules. The last line of this macro is a %include statement that run a utility module applog.sas. This line occurs in most of our SAS web applications. It writes a summary line to a file/dataset that allows us to keep summary stats regarding who, how many, when, etc. these apps get run.