I'd create a subtransformation for the PID segment that perform the required PID transformation logic you described.
Then, in every message transformation you develop insert the if condition using Lookup Table as per @Julian Matthews sample above and call your PID subtransformation.

In this way you have a single place where your PID manipulation is defined with all the advantages that comes with it.

As usual, make sure the subtransformation is called in the proper order/sequence within your message transformation.

It's long time I don't install Ensemble 2018, why are you still using Ensemble and not IRIS?

I just tested installing Ensemble 2018 and CSP Gateway is properly installed and configured.

When the setup kit is run the first dialog is:

Do you get the same dialog? What option you choose?
As you can see it is also possible to install only the CSP Gateway component.

If you choose Custom then this dialog is displayed:

As you can see the option to install CSP Gateway for IIS is there.

If I proceed to the install, at the end I have the CSP Gateway properly configured in C:\inetpub\CSPGateway

In order to help you, apart from saying that is not being installed, can you provide some information on what you are actually doing and what do you actually see/get during install?

I think you need to enable/allow Unauthenticated access for Web Applications.

I can see that in Web Application configuration in "Allowed Authentication Methods" the option "Unauthenticated" is not available, this means is not enabled/allowed systemwide.

In Management Portal check System Administration > Security > System Security > Authentication/Web Session Options, and see if "Allow Unauthenticated access" (first option/checkbox) is enabled.

After that, enable the option "Unauthenticated" in the "Allowed Authentication Methods" of the Web Application.

The WRITE command, when passed no argument, does a reflexion on the system.

Well, it simply list/write all defined variables in current stack level, something you can perform using standard Object Script commands and functions, no need to look at Write source code.

To do that you can use $order,  $query, and indirection, with some trick needed for the variables you use to perform the listing, or, maybe simpler, you can use output redirection (to a string for example) and then...well, an argument less Write command 😊

Some hint for the first option:

USER>set a=1,b=2,b(1)=3,x=""
 
USER>set x=$order(@x)
 
USER>write x
a
USER>set x=$order(@x)
 
USER>write x
b
USER>set x=$query(@x)
 
USER>w x
b(1)

For output redirection, see IO Redirect in ObjectScript in Open Exchange.

Here is a sample method to deflate without using file I/O and using XDEV instead.

ClassMethod UnDeflate(Compressed As %String, Output UnDeflated As %String) As %Status
{
    Set sc=$$$OK
    Try {
        ; open XDEV device
        Set dev="|XDEV|"_+$JOB
        Open dev:($ZF(-6,$$$XSLTLibrary,12):/HOSTNAME="XSLT") Use dev
        ; write compressed string
        Write Compressed
        ; flush buffers
        Write *-3
        ; change device mode to deflate and rewind
        Use dev:(:/COMPRESS="DEFLATE":/POSITION=0)
        ; read uncompressed content
        Read UnDeflated
    } Catch CatchError {
        #dim CatchError as %Exception.SystemException
        Set sc=CatchError.AsStatus()
    }
    Close dev
    Quit sc
}

I really wish that InterSystems implement the various compress/deflate/gzip functionality available in device I/O as utility methods. Without silly wrappers! 😉

Another wish, please InterSystems document XDEV device I/O.