8
Functions, Loops, Arrays, Macros

In this chapter, we study ways to make things more automated by using functions in R and Macro Language in SAS. In addition, we study loops (for and do) as well as arrays in SAS.

8.1 Functions

In R Functions in R are quite easy in Syntax. They are of the form:

FUNCTIONNAME+function(x,y,..parameters){ function description)

They are called by FUNCTIONNAME(input value)

In SAS we use Proc FMCP.

The FCMP procedures are used to write, compile, and test DATA step functions and CALL routines that can be used in DATA step. For more information, you can read http://support.sas.com/resources/papers/proceedings13/139‐2013.pdf

8.2 Loops

Loops are used for tasks that are repeatable. You can also use functions within iterations of loops. Note in SAS you need to use end to close loop and output to output data.

In SAS:

In R:

8.3 Arrays

A SAS array is a way of creating a temporary group of variables for processing within a data step for repeatable operations. Arrays help simplify SAS code readability.

Example:

array balance[12] bal1-bal12;

Here array named balance has 12 variables from variable bal. 1 to bal. 12.

Or

ARRAY months {12} $ 12 mth1-mth12 ('January' 'February' 'March' 'April' 'May' 'June' 'July' 'August' 'September' 'October' 'November' 'December');.

You can also use * within the number of variables [].

8.4 Macros

The Macro Language is a useful and unique tool within SAS language which helps to reduce SAS code, improve readability and pass data in an automated manner. It is composed of Macro variables and Macro Programs.

Let us take a basic Macro.

Let’s makes a global macro variable (outside the declared macro)

%PUT: This statement writes the text in SAS log.

We can also do string operations using Macro variables.

  1. %upcase: converts into upper case.
  2. %substr: returns characters from specified position.
  3. %scan: returns the variable whose number is specified.

Data _null_: To gain higher efficiency in a SAS program, creation of additional dataset can be avoided using this statement. It creates a null dataset.

CALL SYMPUT: This function assigns data step information to a macro variable. It has two parts:

  1. This specifies a character expression that identifies the macro variable which is assigned a value. If the macro variable does not exist, the routine creates it.
  2. This specifies a character constant, variable, or expression that contains the value that is assigned.

8.5 Quiz Questions

  1. How do we close a do loop in SAS?
  2. How we get output in a do loop in SAS?
  3. How do we create a function with two parameters in R?
  4. How do we close a macro in SAS?
  5. What command in data step helps create a null dataset?
  6. How do I output macro variables in SAS log?
  7. With which option do we print out macro variables in an SAS log?
  8. Which function gives data step information to a macro variable?
  9. Which function can be used to create output in excel format in SAS?
  10. Which function can be used to create a global macro variable?

Quiz Answers

  1. End
  2. Output
  3. function(x,y){commands}
  4. %mend
  5. Data _null_
  6. %put
  7. Symbolgen
  8. Call symput
  9. ODS HTML
  10. %let
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.139.80.216