Creating Temporary Files

Overview of Creating Temporary Files

The Application Dispatcher program creates a temporary file for each request. When multiple requests are run at the same time, one request might write over the temporary file of another. There are several ways to create a unique file for the duration of a request so that files do not interfere with each other:

Creating a File with a Unique Name

To create a uniquely named file, use a random function to generate the filename. Then, use the name in a FILENAME statement:
/* Use a data step function RANUNI to generate a random number
and format the number with some text to create the unique name.
The RAN macro variable will be something like 'A2578900.txt' */

data _null_;
   numb = ceil (ranuni(0)*10000000);
   r = 'A' || put(numb, Z7.) || '.txt';
   call symput ('ran', r);
run;

filename foo "c:\temp\&ran";
/* prepend the temp subdirectory */

data _null_;
   file foo;
   put 'This is output to a uniquely named file';
run;
This technique requires that you explicitly delete the file when you are finished.

Creating a File in a Unique Subdirectory

You can avoid file interference by using a unique subdirectory. Beginning with Version 8 of SAS/IntrNet Application Dispatcher, the WORK library is unique for each request. Find the path to the WORK library and use it as the subdirectory to store the temporary file.
Note: To append text to a macro variable reference, use the period (.) operator.
/* use pathname function to get work library's path and store
in macro variable wpath */

%let wpath=%sysfunc(pathname(work));

/* use wpath macro variable and append filename to it in
filename statement */

filename foo "&wpath.\winapi.txt";

data _null_;
file foo;
put 'This output is going to file in the work subdirectory';
run;
If the SESSIONS feature is used, then you can specify the path of the SAVE library:
%let spath=%sysfunc(pathname(save));
filename foo "&spath.\winapi.txt";
This technique deletes the file automatically at the end of the request.

Storing a File in a Unique Catalog

You can create a unique file for each request by storing the file in a unique SAS catalog. Text can be easily stored in a catalog SOURCE memtype by using the CATALOG access method.
filename foo catalog "work.mytext.foo.source";
data _null_;
file foo;
put 'This output is going to file in the work catalog';
run;

Example

You can use any one of these techniques in an Application Dispatcher program to create unique files for each request. To put it all together, here is an Application Dispatcher program that reports the user name for the current process:
/*****************************************************************/
/*              S A S   S A M P L E   L I B R A R Y              */
/*                                                               */
/*        NAME: HelloWorld with a twist                          */
/*       TITLE: Hello World                                      */
/*     PRODUCT: SAS/IntrNet (Application Dispatcher)             */
/*      SYSTEM: ALL                                              */
/*        KEYS:                                                  */
/*       PROCS:                                                  */
/*        DATA:                                                  */
/*                                                               */
/*     SUPPORT: Web Tools Group             UPDATE: 20JAN1999    */
/*         REF: http://support.sas.com/rnd/web/intrnet/dispatch/ */
/*        MISC:                                                  */
/*****************************************************************/
/* create macro variable containing the path to the work subdirectory */
%let wpath=%sysfunc(pathname(work));
%put &wpath;

filename sascbtbl "&wpath.\winapi.txt";

/* write WINAPI GetUserNameA parameter list to the file for later use */

data _null_;
file sascbtbl;
input line $char80.;
put line $char80.;
cards4;
routine GetUserNameA
minarg=2
maxarg=2
stackpop=called
module=advapi32
returns=short;
arg 1 char update format=$cstr20;
arg 2 num update format=pib4.;
;;;;
run;

/* Here's the DATA step: Modulen will use the filename sascbtbl for
parameter list to the called API */

data _null_;
   length Name $20.;
   name='';
   Size=20;
   rc=modulen('GetUserNameA',Name,Size);
   put rc= Name=;

/* Store the current process user name in the macro named vqpname */

   call symput('vqpname',name);
run;

/*simply write out a Web page that says "Hello World!"*/

/* use a DATA step variable and a macro variable in displayed text */

data _null_;

/* store macro variable value in data set variable named vname */

vname=symget('vqpname');
   file _webout;
   put '<HTML>';

/* Use data set variable in the output */

   put '<HEAD><TITLE>Hello World!' vname ' is running dispatcher!
      </TITLE></HEAD>';
   put '<BODY>';

/* Use macro variable in the output */

   put "<H1>Hello World! Your work path is &wpath.
      vname is &vqpname.</H1>";
   put '</BODY>';
   put '</HTML>';
run;