DATA Step, Macro, Functions and more

Meta alternative to hard coded values in calculations?

Posts: 0

Meta alternative to hard coded values in calculations?


First off, i´m new to SAS but i have some experience with similar areas. In the project i´m currently involved in, i was asked to look into the possibilities to change the hard coded calculations made in different models, to instead be put in a more manageable way in "meta-tables", which then could be easily versioned separately.

My idea was to solve the calculation of for instance the following:

if theInput = 14 then theOutput=0;
if theInput > 90 then theOutput=90;

to be calculated as follows instead:

theOutput = calculateMe('theInput',theInput);
where calculateMe has access to a "meta-table" holding the specific calculation logic for the variable 'theInput' (i.e having information about what to return for various values of theInput), enabling it to return the correct value in a generic way.

Do i make any sense to anyone and if so, does anyone know of any "best practices" for implementing this type of "pattern"? Perhaps this is a basic approach for anyone familiar to SAS or statistical programming but i haven´t found anything (sugi or such) on this subject yet. Thanks in advance!
Trusted Advisor
Posts: 2,113

Re: Meta alternative to hard coded values in calculations?

A lot of that can be done using the SAS MACRO language. In addition to the manuals, I'd suggest looking at Rick Aster's book on SAS programming shortcurts and one of the BBU's on SAS macro language that are available in the SAS bookstore.

Doc Muhlbaier
Posts: 0

Re: Meta alternative to hard coded values in calculations?

Thank you for the answer, i will look into the macro language to learn more.

It would however still be interesting to know if there are any best practices for realizing this type of pattern since i have a feeling that it must have been done in various projects before.

Kind regards,
Super Contributor
Posts: 359

Re: Meta alternative to hard coded values in calculations?

Based on what you are showing, I would create a format using my meta-table as a cntrlin data. then recode based on the format.
Respected Advisor
Posts: 3,887

Re: Meta alternative to hard coded values in calculations?


If the rules are as simple as in your example then Flip's suggestion sounds good to me.

If the rules are more complex then one way I have seen this done is by storing the rules in a SAS table.
During execution a first data step (data _null_) reads the rules table and creates SAS code which gets 'outputted' to a temporary file. After that use an %include for this temporary file and the generated SAS code will be executed.

SAS macro language is a very powerful addition to Base SAS language. But I strongly recommend that you first get familiar with Base SAS language.

Posts: 0

Re: Meta alternative to hard coded values in calculations?


Thanks for all the answers - i actually did not think of the solution using formats but that can surely be a good way to realize some of our needs. However, there are, as you mention (but i forgot to), much more complex needs that need a more complex solution and it is here that the "meta table" (not sure this is the right name for it though, hope you know what i mean anyway) comes handy. Using this by importing it in a pre-step before executing the code looks like a good technical/structural solution. The main issue here is that i believe there could/should be a generic way to design such a table which we, in order to prevent reinvention of the wheel, would be really interested in.

Some of the logic (no bore-to-death try attempted) we are looking to solve are for instance also:

if 0 GT (inputOne - inputTwo) LT 1024
then TheOutPut = oneMoreVariable * 1.5;
else if 2048 GT (inputOne - inputTwo) LT 4096
then TheOutPut = oneMoreVariable * 1.66;

which is a more challenging and complex due to the need to represent combinations of expressions in the metadata. This can surely be done, and since there are no rocket science related expressions - rather just simple expressions stacked together, this should be possible to realize in a meta table.

Just a clarification, i am by no means writing here to avoid doing the work myself, i´m just curious if there are any general approaches for dealing with this type of data-driven design since i feel there are a lot of mistakes possible to make when trying to design this table.

Kind regards,
Posts: 8,742

Re: Meta alternative to hard coded values in calculations?

Some of what you describe sounds like fairly standard SAS Macro programming and the implementation of business rules sounds like the kind of functionality built into Info Map Studio and Metadata as implemented in the SAS Intelligence Platform (aka the BI Platform). SAS already uses the concept of METADATA in the Platform.

There are many papers about Metadata, as implemented on the Platform. There are probably even more papers written where folks describe their totally awesome SAS Macro applications. Googling around on these search strings should get you started
SAS Metadata BI Platform information maps

SAS Macro application example

Ask a Question
Discussion stats
  • 6 replies
  • 5 in conversation