As the first step in the decommissioning of sasCommunity.org the site has been converted to read-only mode.


Here are some tips for How to share your SAS knowledge with your professional network.


Get the Metadata Path for the Currently Running Stored Process

From sasCommunity
Jump to: navigation, search

Overview

The SAS/IntrNet® Application Server creates reserved macro variables which are the individual components of the _PROGRAM macro variable. For example, you can reference another program in the same folder by using the macro variable _PGMLIB. However, no such facility exists in version 9.1.3 of the SAS Stored Process Server.

Starting with SAS®9.2 a new macro variable named _METAFOLDER is available. The value of the variable is the SAS metadata path for the currently running stored process. This allows a program to reference another stored process in the same folder without having to hard-code the entire path, or parse the path value from the _PROGRAM macro variable. For example:

put "<input type=""hidden"" name=""_program"" value=""&_METAFOLDER.Another Stored Process"">";

In version 9.1.3, you can use the storedProcessPath macro (source code below) to get the path value for the currently executing stored process. The macro can also traverse the SAS metadata folder hierarchy and return a higher level path.

Using the storedProcessPath Macro

The macro operates as a function in that it returns a value. The macro can be referenced directly in a PUT statement to generate a value for another stored process in the same SAS metadata folder, e.g.,

put "<input type=""hidden"" name=""_program"" value=""%storedProcessPath.Another Stored Process"">";

For example, if the stored process that includes the above PUT statement is /Projects/My App/Entry Point, the above PUT statement will generate:

<input type="hidden" name="_program" value="/Projects/My App/Another Stored Process">

The macro can also be invoked in such a way that the path is saved as the value of a macro variable:

%let myPath = %storedProcessPath;

Macro Arguments

The storedProcessPath macro accepts one argument, levels, that determines how far up the hierarchy to traverse before returning the path. The value of levels must be zero (0) or a positive integer. If the value contains any non-numeric characters the default value, zero (0), is used.

The value returned by the macro for various values of levels is illustrated by the following examples. Suppose the current stored process name and path stored in the _PROGRAM macro variable is /BIP Tree/Projects/Query Warehouse/Summary Report. The table below shows the path returned based on different values of the levels argument.

Levels Path
0 (the default) /BIP Tree/Projects/Query Warehouse/
1 /BIP Tree/Projects/
2 /BIP Tree/
3 /
>3 /

Referencing a Stored Process in Another Folder

Consider the example above, where the current stored process name and path stored in the _PROGRAM macro variable is /BIP Tree/Projects/Query Warehouse/Summary Report. The Summary Report stored process needs to reference a generic stored process called Listing in a sub-folder of the Query Warehouse folder. That is, you have a sub-folder of the Projects folder named Tools, and it contains the Listing stored process.

The Summary Report stored process can refer to the /BIP Tree/Projects/Tools/Listing stored process as follows:

%storedProcessPath(levels=1)Tools/Listing

The stored process path is not hard-coded , facilitating maintenance.

storedProcessPath Macro Source Code

The macro source follows. This macro may be freely used and distributed as long as the comment block that references this page (Get the Metadata Path for the Currently Running Stored Process), as it's original source is preserved in any copies that are made or distributed.

%macro storedProcessPath
       (levels=0           /* how many levels up in the hierarchy to go */
       );
 
 /*------------------------------------------------------------------------------------------
   DEVELOPED BY : Don Henderson
   PURPOSE      : Determines the path for the current stored process. This macro is used to
                  generate a value for _program that is another stored process in the same
                  directory as the current stored process. Use of this macro avoids having
                  to hard code the path in the actual Stored Process.
 
                  The levels parameter allows you to specify how many levels up in the folder
                  hierarchy to go. The default is 0 which causes the return value to be the
                  current folder.
 
 |------------------------------------------------------------------------------------------|
 |  MAINTENANCE HISTORY                                                                     |
 |------------------------------------------------------------------------------------------|
 |  DATE    |     BY    | DESCRIPTION                                                       |
 |----------|-----------|-------------------------------------------------------------------|  
 | Dec 2006 |  Don H    | Initial Creation
 | Mar 2009 |  Don H    | Add option to go up a specified number of levels in the hierarchy
 |-----------------------------------------------------------------------------------------*/
 
 %global _metafolder;  /* new server created macro variable in 9.2 */
 %global _program; /* ensure it exist */
 %local i storedProcessPath sepChar;
 
 %let sepChar = /;
 
 %if %length(%sysfunc(compress(&levels,%str(),d))) gt 0 %then
 %do;  /* non-numeric - use default */
    %put WARNING: Levels value, &levels, contains non-numerics. Default will be used.;
    %let levels = 0;
 %end; /* non-numeric - use default*/
 
 /* determine the current folder */
 %if %length(&_metafolder) gt 0 %then %let storedProcessPath = &_metafolder;
 %else %if %sysfunc(index(&_program,&sepChar)) = 0 %then %let storedProcessPath = &sepChar;
 %else %let storedProcessPath = %substr(&_program,1,%sysfunc(findc(&_program,&sepChar,-1024)));
 
 /* make sure the leading separator char is there */
 %if %qsubstr(%superq(storedProcessPath),1,1) ne %superq(sepChar) %then %let storedProcessPath = &sepChar&storedProcessPath;
 
 /* traverse the folder hierarchy the number of desired levels. Above code sets the current path */
 %let i = 1;
 
 %do %while(&i le &levels and %length(&storedProcessPath) gt 1); 
    /* strip off the trailing separator */
    %if &i ne 0 %then %let storedProcessPath = %substr(%superQ(storedProcessPath),1,%length(%superQ(storedProcessPath))-1);
    /* find the last separator character */
    %let storedProcessPath = %substr(%superQ(storedProcessPath),1,%sysfunc(findc(%superQ(storedProcessPath),&sepChar,-1024)));
    /* if at the top (i.e., Path is just the leading separator), force the loop to end */
    %if %superq(storedProcessPath) = %quote(&sepChar) %then %let i = &levels;
    %let i = %eval(&i + 1);
 %end;
 
 /* return the value */
 &storedProcessPath
 
%mend storedProcessPath;

Note that the macro source is included directly in this article instead of as a file that can be downloaded. This allows others to change and/or update the macro to improve it, while allowing anyone to access any prior version using the history tab at the top of this page.

Original macro source contributed by Henderson Consulting Services.

Assumptions

The operation of the storedProcessPath macro assumes that the values of the _METAFOLDER and _PROGRAM macro variables are valid.

The values of these macro variables can be altered in your SAS code (not a good practice, but possible), and if the values are not valid, the macro may fail.

For example, if you altered the value of _PROGRAM:

  • In version 9.1.3, the macro operates on that altered value,
  • In SAS®9.2, the macro operates on the value of _METAFOLDER.