- [The GSM package](#gsm-package)
- [Content description](#content-description)
  * [`%GSM()` macro](#gsm-macro)  
  * [`%GSMpck_makeFCMPcode()` macro](#gsmpck-makefcmpcode-macro)  

  * [License](#license)
  
---


# The GSM package [ver. 0.16] <a name="gsm-package"></a> ###############################################

The **GSM** (a.k.a. *Generate Secure Macros*) package allows
to create secured macros stored in SAS Proc FCMP functions.
The dataset with functions can be shared and allows to generate 
macros without showing their code.

The GSM package is basically an automated version of the following:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~sas
proc fcmp outlib = work.gsm.secure ENCRYPT;
  function generateMacro() $;
    rc = RESOLVE('
      %macro secretMacro(x) / SECURE;
        data test;
         a = "&x.";
        run;
      %mend;
    ');
    return (rc);
  endsub;
run;

/* share work.gsm dataset */
options cmplib = work.gsm;
data _null_;
  rc = generateMacro();
  put rc=;
run;

/* enjoy */
%secretMacro(42)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
See examples for more details.


*How to use it:* 
 - Copy all files with your secured macros code into a directory. 
   Best approach is to have one file for one macro.
 - Copy a path to the directory.
 - Run the following code:
   ```
   %GSM(<the path to directory>, cmplib=<name of the dataset>)
   ```
 - Share generated `ZIP` file (unzip and run the code).
 
*Limitations:* 
 Single macro file cannot be longer than 32760 bytes.

---

Package contains: 
  1.   macro      gsm 
  2.   macro      gsmpck_makefcmpcode 

Required SAS Components: 
  `Base SAS Software`

* SAS package generated by generatePackage, version 20211111 *

The SHA256 hash digest for package BasePlus: 
`96B8E8EE268403D81BBB2740A0F47B835EB480F16B47A9B51FD976DEE3595ED7` 

 
## >>> `%GSM()` macro: <<< <a name="gsm-macro"></a> #######################

The `%GSM()` macro is the main macro of 
the **GSM** (a.k.a. *Generate Secure Macros*) package. 

It converts a list of macros provided by the user into
a data set of the Proc FCMP functions. The macros are stored
in functions are encrypted which allow to share them without 
showing their code. *Important* thing is that macros provided 
by the user *has* to be "secure", i.e. the `secure` option has to 
be added to the macro definition. See the example:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~sas
%macro secretMacro(x) / SECURE; /* <- the secure option */
  <... some code ...>
%mend secretMacro;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


As a result a zip file, containing dataset with functions and 
code to be executed on site, is generated. 

Since encrypted code is stored in a SAS dataset it has
no limitation in sharing between operating systems (like catalogs have).

*Limitation:* Due to the `Resolve()` function limitations 
 a single macro file cannot be longer than 32760 bytes.

*Notes:* 
- All macros have to have the `secure` option added, i.e. `%macro aMacroname(...) / SECURE ;`.
- During the execution a test macro, named `%GSMpck_dummyMacroForTests()`, is generated.
- The `%GSM()` macro calls the `%GSMpck_makeFCMPcode(...)` macro internally.

### SYNTAX: ###################################################################

The basic syntax is the following, the `<...>` means optional parameters:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~sas
%GSM( 
   path
 <,trim=0>
 <,cmplib=work.generateMacros>
 <,source2=>
 <,outpath=>
 <,encodingRestricted=>
)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

**Arguments description**:

1. `path`               - *Required*, indicates a directory which contains files with macros.
                          Only files with `sas` extension are used.

* `trim=`               - *Optional*, the default value is `0`.
                          If set to `1` then lines of macro code are trimmed.
                          If set to `2` then lines of macro code are stripped.

* `cmplib=`             - *Optional*, the default value is `work.generateMacros`. 
                          Names the dataset which will contain generated functions. 

* `source2=`            - *Optional*, the default value is null. 
                          Indicate if `%includ`-ed files are printed out.
                          Any value other than null enables printing.

* `outpath=`            - *Optional*, the default value is set the same as the `path`.
                          Points a directory in which a result (a zip file) is generated.

* `encodingRestricted=` - *Optional*, the default value is `0`.
                          If set to 1 then if User session encoding is different from 
                          encoding of the session which generates the dataset then 
                          the generateMacros() function will not execute macro code.

---


### Example: ###################################################################

Example 1. Prepare 2 files: `f1.sas` and `f2.sas` and use the `%GSM()` macro.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~sas
%let path = %sysfunc(pathname(work))/path2files;

%put &=path.;
options dlcreatedir;
libname path "&path.";
filename path "&path.";

data _null_;
  file path(f1.sas);
  input;
  put _infile_;
cards4;
%macro abc(x) / SECURE;
  data test;
    do i = 1 to &x.;
      put i=;
    end;
  run;
%mend;
;;;;
run;

data _null_;
  file path(f2.sas);
  input;
  put _infile_;
cards4;
%macro xyz(x) / SECURE;
  %do i = 1 %to &x.;
    %put &=i;
  %end;
%mend;
;;;;
run;

%GSM(&path., cmplib=work.myMacros)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

 
## >>> `%GSMpck_makeFCMPcode()` macro: <<< <a name="gsmpck-makefcmpcode-macro"></a> #######################

The `%GSMpck_makeFCMPcode()` macro is an internal macro of 
the **GSM** (a.k.a. *Generate Secure Macros*) package. 

It executes a process of converting 
a macro provided by the user into
a Proc FCMP function. 

Since encrypted code is stored in a SAS dataset it has
no limitation in sharing between operating systems (like catalogs have).

*Limitation:* Single macro file cannot be longer than 32760 bytes.

### SYNTAX: ###################################################################

The basic syntax is the following, the `<...>` means optional parameters:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~sas
%GSMpck_makeFCMPcode( 
   path
  ,number
 <,trim=0>
 <,outlib=work.generateMacros.secure>
 <,source2=>
 <,fileNameCode=FNC>
)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

**Arguments description**:

1. `path`         - *Required*, indicates a directory which contains files with macros.
                    Only files with `sas` extension are used.

2. `number`       - *Required*, a sequential number.

* `trim=`         - *Optional*, the default value is `0`.
                     If set to `1` then lines of macro code are trimmed.
                     If set to `2` then lines of macro code are stripped.

* `cmplib=`       - *Optional*, the default value is `work.generateMacros`. 
                    Names the dataset which will contain generated functions. 

* `source2=`      - *Optional*, the default value is null. 
                    Indicate if `%includ`-ed files are printed out.
                    Any value other than null enables printing.

* `fileNameCode=` - *Optional*, the default value is `FNC`.
                    Internal fileref.

---


## License ####################################################################

Copyright (c) Bartosz Jablonski, since 2021

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

---