Efficient Coding Techniques In SAS®

As SAS® software becomes increasingly more popular, guidelines for its efficient use is critical. The Base-SAS software provides SAS users with a powerful programming language for accessing, analyzing, manipulating, and presenting data. Addressing useful and efficient coding techniques for SAS users on all operating system platforms becomes inevitable as coding with less CPU usage time, less errors and warnings and using SAS functions, PROCS and arrays makes the programming not only faster but also reduces long codes which are not very promising and increases chances of redundancy. For example, many SAS users at preliminary level are found to be writing programs the hard way. If they only learned about dates or functions or procedure or learned how to use the available resources, they can save themselves a lot of time and frustration. However, when using SAS software, you quickly discover that there are many ways to achieve your results.

The idea behind efficient programming should be 1) simple 2) efficient 3) making maximum use of already available SAS functions and procedure steps or standard nonstandard macros depending upon the work assigned. Almost every SAS programmer uses ‘If then else’ statements often to implement conditional logics in the program. IFC, IFN functions are great alternatives to the regular ‘if then else’ statements. These functions are very handy to use. These functions help in reducing the lines of code and helps in handling missing values.

IFC Function: The IFC function returns a character value based on whether an expression is true, false, or missing. Syntax: IFC(logical-expression, value-returned-when-true, value-returned-when-false <, value-returned-when-missing>)

IFN function: The IFN function returns a numeric value based on whether an expression is true, false, or missing. Syntax: IFN(logical-expression, value-returned-when-true, value-returned-when-false<, value-returned-when-missing>)

Example for IFC:

To derive treatment group variables (TRTA and TRTP). Based on the respective APERIOD values, TRTA and TRTP values were assigned from TRT01A/TRT01P, TRT02A/TRT02P, TRT03A/TRT03P. Suppose if APERIOD is 1 then TRTA/TRTP will be TRT01A/TRT01P etc.

pic1_sas

pic2sas

pic3sas

pic4sas

Example for IFN:

To derive VISITNUM from VISIT variable. Sample visit data as follows:

pic5sas

pic6sas

pic7sas