How to build a vector from expressions


If you are a statistical programmer, sooner or later you have to compute a confidence interval. In the SAS/IML language, some beginning programmers struggle with forming a confidence interval. I don't mean that they struggle with the statistics (they know how to compute the relevant quantities), I mean that they struggle with constructing the SAS/IML vector that contains the lower and upper endpoints.

To be specific, suppose you compute some quantities, x and δ, and you want to form the 2x1 vector (x–δ, x+δ). You might be tempted to write the following SAS/IML statements:

CI = { x-delta, x+delta }; /** WRONG **/

What is wrong with this statement? In the SAS/IML language, curly braces are used to build matrices from literals only. (In computer programming, a literal is just a fancy name for a fixed constant.) You can form a numeric matrix from numeric constants such as -1, 1.23, 2E-2, or the SAS numerical missing value, "." Character matrices are formed from string constants such as "Dog" and "Cat."

So how do you create a vector from expressions or from values that are contained in scalar variables? You use the horizontal concatenation operator (||) or the vertical concatenation operator (//), as follows:

CI = x-delta // x+delta;

The concatenation operators have lower precedence than the arithmetic operators, so the previous statement does not require parentheses.


About Author

Rick Wicklin

Distinguished Researcher in Computational Statistics

Rick Wicklin, PhD, is a distinguished researcher in computational statistics at SAS and is a principal developer of SAS/IML software. His areas of expertise include computational statistics, simulation, statistical graphics, and modern methods in statistical data analysis. Rick is author of the books Statistical Programming with SAS/IML Software and Simulating Data with SAS.

1 Comment

  1. Pingback: A simple trick to construct symmetric intervals - The DO Loop

Leave A Reply

Back to Top