Published on :

Update Stored Process Server Type

This code is also available in: Deutsch Español Français
Attention : This code requires administrator privileges.
The `%mm_updatestpservertype` macro is designed to modify the server type of an existing SAS© Viya 4 stored process. It requires two parameters: `target` (the full path to the stored process) and `type` (either 'WKS' for a Workspace server, or 'STP' for a Stored Process server).
The macro execution proceeds in several steps:
1. Existence Check: A `DATA _NULL_` block uses the `metadata_pathobj` function to query SAS© metadata to verify if a stored process exists at the `target` path and to extract its URI and type. If the object is not identified as a stored process, the macro stops with a warning message.
2. New Type Determination: A conditional macro logic `%if %then %let` is used to translate the `type` parameter into an appropriate value ('Wks' or 'Sps') for the metadata.
3. Metadata Update: A second `DATA _NULL_` block iterates through the metadata associations (`Notes`) of the stored process URI using `metadata_getnasn` and `metadata_getattr`. When the 'Stored Process' note is found, the `METADATA_SETATTR` function is used to modify its 'StoredText' attribute. This attribute contains an XML structure that is updated to include the chosen `LogicalServerType`. The `OtherAllowed="false"` parameter is also set.
4. Result Reporting: Finally, the macro displays a success or error message in the SAS© log, based on the result of the metadata update operation. This operation is considered administrative because it modifies the configuration of stored processes at the metadata server level.
Data Analysis

Type : AUCUNE


The macro does not use SAS data (tables or datasets) as a source. It operates directly on the SAS metadata system, querying and modifying the definitions of stored processes. Therefore, there is no dependency on external data or specific libraries for its functional execution.

1 Code Block
DATA STEP
Explanation :
This `DATA _NULL_` block is used to interact with the SAS metadata server. The `metadata_pathobj` function attempts to locate the stored process specified by the `target` parameter and extract its type (`cmtype`) and URI (`stpuri`). This information is then stored in local macro variables. A check is performed to ensure that the found object is indeed a 'StoredProcess'. If not, a warning is issued and the macro terminates.
Copied!
1DATA _null_;
2 LENGTH type uri $256;
3 rc=metadata_pathobj("","&target",'StoredProcess',type,uri);
4 call symputx('cmtype',type,'l');
5 call symputx('stpuri',uri,'l');
6RUN;
7%IF &cmtype ne ClassifierMap %THEN %DO;
8 %put %str(WARN)ING: No Stored Process found at ⌖
9 %return;
10%END;
2 Code Block
MACRO LOGIQUE
Explanation :
This simple macro logic block determines the server type value to set. If the `type` parameter is 'WKS', the `newtype` macro variable is set to 'Wks'. Otherwise, it is set to 'Sps'. This variable will be used later to construct the XML string for metadata update.
Copied!
1%IF &type=WKS %THEN %let newtype=Wks;
2%ELSE %let newtype=Sps;
3 
3 Code Block
DATA STEP
Explanation :
This second `DATA _NULL_` block is the core of the update logic. It iterates over the notes associated with the stored process URI (`stpuri`) using `metadata_getnasn`. It specifically searches for the note named 'Stored Process'. Once found, the `METADATA_SETATTR` function is used to modify its 'StoredText' attribute. This attribute is updated with a new XML string that defines the `LogicalServerType` of the stored process using the `&newtype` macro variable determined previously. The success of this operation is recorded in the `result` macro variable.
Copied!
1DATA _null_;
2 LENGTH uri name value $256;
3 n=1;
4 DO while(metadata_getnasn("&stpuri","Notes",n,uri)>0);
5 n+1;
6 rc=metadata_getattr(uri,"Name",name);
7 IF name='Stored Process' THEN DO;
8 rc = METADATA_SETATTR(uri,'StoredText'
9 ,'<?xml version="1.0" encoding="UTF-8"?>'
10 !!'<StoredProcess><ServerContext LogicalServerType="'!!"&newtype"
11 !!'" OtherAllowed="false"/><ResultCapabilities Package="false" '
12 !!' Streaming="true"/><OutputParameters/></StoredProcess>');
13 IF rc=0 THEN call symputx('result','SUCCESS');
14 stop;
15 END;
16 END;
17RUN;
4 Code Block
MACRO LOGIQUE
Explanation :
This final block is a conditional macro statement that displays a message in the SAS log. If the `result` macro variable is 'SUCCESS', a confirmation message is displayed, indicating that the stored process server type has been modified. Otherwise, a general error message is issued, signaling a problem during macro execution.
Copied!
1%IF &RESULT=SUCCESS %THEN %put NOTE: SUCCESS: STP &target changed to &type type;
2%ELSE %put %str(ERR)OR: Issue with &sysmacroname;
3 
This material is provided "as is" by We Are Cas. There are no warranties, expressed or implied, as to merchantability or fitness for a particular purpose regarding the materials or code contained herein. We Are Cas is not responsible for errors in this material as it now exists or will exist, nor does We Are Cas provide technical support for it.
Copyright Info : Copyright information is detected in the referenced files: 'Copyright (c) 2001-2006 Rodney Sparapani' (under GNU General Public License) for _version.sas, and 'Copyright 2010-2023 HMS Analytical Software GmbH, http://www.analytical-software.de' (part of SASUnit) for macro_without_brief_tag.sas. These references are included in the main macro's help block.


Banner
Expert Advice
Expert
Simon
Expert SAS et fondateur.
« The %mm_updatestpservertype macro illustrates an advanced administrative technique: direct modification of a Stored Process execution configuration. While the description mentions SAS Viya 4, the code actually utilizes the Open Metadata Architecture (OMA) specific to SAS 9.x. By manipulating the StoredText attribute, you are directly instructing the Object Spawner on how to instantiate the SAS engine to fulfill a user request.

Strategic Insights & Best Practices
WKS vs. STP (SPS): Choosing the right server type is vital for performance. WKS (Workspace Server) is ideal for heavy lifting with dedicated sessions, whereas STP (Stored Process Server) uses a pool of multi-user sessions, providing faster response times for frequent, lightweight requests.

The Risk of Overwriting StoredText: The METADATA_SETATTR function in this script injects a hardcoded XML string. Warning: This method overwrites the entire attribute. If your Stored Process had custom configurations (specific result capabilities, input parameters, etc.), they will be lost. An expert approach would be to read the existing XML first and only modify the LogicalServerType attribute.

Metadata Persistence: Unlike changes made via the SAS Management Console, modifications via METADATA_SETATTR are immediate and permanent in the repository. Always perform a metadata export (SPK) of the object before running automation macros that modify attributes. »