We’re smarter together. Learn from this collection of community knowledge and add your expertise.

Tip: Commonly Used Macros and Macro Variables in SAS Code Node

by SAS Employee ilknurkabul on ‎05-22-2014 09:08 AM - edited on ‎10-06-2015 02:27 PM by Community Manager (2,572 Views)

The SAS Code node enables you to incorporate new or existing SAS code into process flow diagrams that were developed using SAS® Enterprise Miner™. The Code Editor in SAS Code node provides tables of built-in macros and macro variables that you can use to integrate your SAS code with the SAS® Enterprise Miner™ environment.

The Macros table lists the SAS macros that are used to encode multiple values, such as a list of variables, and functions that are already programmed in Enterprise Miner. They can be used to manage data, format outputs and identify variable definitions at run time.



The Macro Variables table lists the macro variables that are used to encode single values such as the names of the input data sets.


You can insert a macro variable, a variables macro, or a utility macro into your code without having to type its name; you simply select an item from the macro variables list or macros table and drag it to the active code pane.


How might it work?


I will demonstrate how to use some commonly used macro variables and macros with a few simple examples.

We first define a data source for the data set used in this example, SAMPSIO.HMEQ. We set the measurement level binary for BAD, and nominal for JOB and REASON. For the other variables, we set the level to interval.



Example 1: Accessing/Modifying data sets

In SAS® Enterprise Miner™, each node runs with its own WORK library on the SAS Workspace Server. A WORK data set that you create in a SAS Code node is not available to a successor node.

If you want the generated data set to be "in line" with the diagram flow, to accept a data set from the predecessor node, and to forward the data set to the successor node, then you should name the data sets in the DATA and SET statements using macro variables. This ensures the data is read and written with names that SAS Enterprise Miner maintains. For example, if you want to modify your training data within the SAS Code node, scroll through the macro variables that are listed in the Imports section. The macro variable that defines the data from the predecessor node is EM_IMPORT_DATA. Do the same for the data set that you want to create by identifying the appropriate macro variable name under the Exports listing.

For example, consider the following flow:


The code node “KeepIntervalInputs” manipulates the data that comes from the predecessor node, HMEQ. It keeps the interval input variables of the data and puts them on the output data set that is going to the successor node,CheckResults.

SAS code in the code node “KeepIntervalInputs”:



       set &EM_IMPORT_DATA (keep= %EM_INTERVAL_INPUT);



Here we used macro variables &EM_IMPORT_DATA and &EM_EXPORT_TRAIN, and variable macro %EM_INTERVAL_INPUT.

  • &EM_IMPORT_DATA — resolves to the name of the training data set imported into the code node (from a predecessor node)
  • &EM_EXPORT_TRAIN — resolves to the name of the training data set exported from the code node (to successor node)
  • %EM_INTERVAL_INPUT — resolves to the interval variables that have a model role of input.

The incoming data set to the successor code node “CheckResults” will only include the interval variables in the hmeq data set. In “CheckResults” you can continue to work on the data set that has only interval input variables.

Example 2: Changing metadata

You can change column metadata data using the utility macro %EM_METACHANGE.

The following flow converts binary variables in the HMEQ dataset to interval variables:


After running this flow, if you check the incoming dataset to the code node “CheckResults”, you will see that the measurement level for BAD is converted to interval.

Here is the SAS code used in the “ConvertBinaryToInterval” node:


%macro binary_to_interval;


       %if (&EM_NUM_BINARY_INPUT) %then %do;

              %do i= 1 %to &EM_NUM_BINARY_INPUT;


                           NAME= %scan(%EM_BINARY_INPUT, &i),

                           LEVEL= INTERVAL                        








In the binary_to_interval macro, we use the macro variables &EM_NUM_BINARY_INPUT, variable macro % EM_BINARY_INPUT and utility macro %EM_METACHANGE.

  • &EM_NUM_BINARY_INPUT — resolves to the number of binary input variables.
  • %EM_METACHANGE — Use the %EM_METACHANGE macro to modify the column metadata data set that is exported by a node. The macro should be called during either the TRAIN or SCORE actions.
  • %EM_BINARY_INPUT — resolves to the binary variables that have a model role of input.


We first check whether the number of binary input variables &EM_NUM_BINARY_INPUT is not equal to zero. If so, we loop through each binary input variable and convert that variable`s level from binary to interval using the%EM_METACHANGE utility macro. We access the ith binary input variable by running %scan(%EM_BINARY_INPUT, &i) inside the loop.


Example 3: Using built-in macro variables to create and access data sets

You can create and access your files related to your code node using the macro variables &EM_LIB and &EM_NODEID.



In this example, we construct a metadata data set &EM_NODEID._bin in the EMWS SAS library &EM_LIB.


SAS code in the code node “GenerateMetaData”:



proc hpdmdb data=&EM_IMPORT_DATA classout=  &EM_LIB..&EM_NODEID._bin;

       class %EM_BINARY_INPUT;



Here we used the macro variables &EM_LIB  and &EM_NODEID.

  • &EM_LIB — resolves to the numbered EMWS SAS library containing the data sets and SAS catalogs related to the current process flow diagram. This will be the same as the value of the process flow diagram's ID property.
  • &EM_NODEID — resolves to the node ID.


PROC HPDMDB creates summaries of the input data source. The CLASSOUT data set &EM_LIB..&EM_NODEID._bin contains a summary of the classification variables which are specified with %EM_BINARY_INPUT in this example.

Here, &EM_NODEID for the demo workflow is ‘emcode.’ So the code above creates the file emcode_bin.sas7bdat, which is shown below, in the project workspace folder.



Example 4: Registering a dataset

If you create a dataset as we did in example 3, the dataset is written to project workspace but is unregistered. Among other things, this means that if you delete the code node from your flow, the dataset is not cleaned up.

In many cases it is useful to register the datasets you create using the %EM_REGISTER macro. If you register a dataset Enterprise Miner will track the state of the file, avoid name conflicts, and ensure that the file is deleted when the node is deleted from a process flow diagram.

Registration involves associating a dataset with a key. For example, if you want to create a dataset sample, you first register the key sample. Later in your code, you can reference to that dataset by using &em_user_sample.



data &em_user_sample;

       do id=1 to 50;

          XVar = 1 + ranuni(1234);

          YVar = 1 + ranuni(1234);



          XVar = 2 + ranuni(1234);

          YVar = 2 + rannor(1234);



          XVar = 3 + ranuni(1234);

          YVar = 3 + rannor(1234);






This registered file is saved to project workspace. By using %EM_REGISTER, you get a key by which you can access the data set. In example 3, you would have to use &EM_LIB..&EM_NODEID._bin to access the data instead of the &EM_USER_key variable.

%EM_REGISTER — Use the %EM_REGISTER macro to register a unique file key. When you register a key, Enterprise Miner generates a macro variable named &EM_USER_key. You then use &EM_USER_key in your code to associate a file with the key.




We’ve seen how to use some of the built-in macros and macros variables within a SAS Code node in Enterprise Miner.


Macros and macro variables are useful to enhance your SAS code with information from the Enterprise Miner environment. As shown in the above examples, they can be used to reference information about the imported data sets, the input variables, the exported data set, the project metadata and so on.


The SAS Code node supports many other macros and macro variables. To learn more about them, see “Appendix 1: SAS Code Node Documentation” in the SAS Enterprise Miner Extension Nodes Developer's Guide:


Your turn
Sign In!

Want to write an article? Sign in with your profile.