How to Create a Simple Table with sprintf in GAUSS

Introduction

This post will show you how to create a simple output table with variable names, parameter estimates and standard errors as shown below, using sprintf.

  population  -0.6558  (0.1910)
poverty rate   0.9465  (0.2505)
   inflation   1.7366  (0.1101)

sprintf, first introduced in GAUSS version 20, is a standard function in many languages. It provides powerful, compact statements that allow you to combine and style numeric and string data.

Unfortunately, some people find the sprintf format string to be difficult to use and understand. Fortunately, today we are going to make it clear for you!

GAUSS sprintf Inputs and Outputs

The GAUSS sprintf function takes two or more inputs and returns a string array.

sprintf(fmt, a)
sprintf(fmt, a, ...)


fmt
A sprintf format string (described later).
a
A scalar, matrix or string array to print.
...
One or more additional matrices, strings or string arrays which have the same number of rows as a.

sprintf Format String Basics

The sprintf format string has many options that give you great control over the presentation of your data. Today we will show you how to use the three most important options which will cover most use cases.

  • The specifier character.
  • The field width.
  • The precision.

Infographic to explain sprintf field width and precision.

sprintf Specifier Character

The most basic format specifier starts with a percent sign (%) followed by a letter which is called the specifier character.

Specifier Character Output Example
d Integer sprintf("%d", 3.1415) 3
e Scientific sprintf("%e", 3.1415) 3.1415e+00
f Decimal sprintf("%f", 3.1415) 3.1415
s String sprintf("%s", "Pi") "Pi"

sprintf format strings can contain more than one format specifier and it can be mixed with text. For example:

sprintf("%s is %d", "Steve", 38);

will print out:

Steve is 38

Now we know enough to create the first iteration of our table.

// Create a 3x1 string array
var_names = "population" $| "poverty rate" $| "inflation";

// Create two 3x1 vectors
beta_ = { -0.65582591,
           0.94648444,
           1.7365981 };

se = {  0.19095146,
        0.25047475,
        0.11009904 };

sprintf("%s %f %f", var_names, beta_, se);

will print out

population -0.655826 0.190951
poverty rate 0.946484 0.250475
inflation 1.736598 0.110099

The overlapping columns make this table difficult to read.

sprintf Field Width

To stop the columns from overlapping, we need to make the field width large enough to hold the widest element for each column. The field width is an integer placed immediately following the percent sign (%). For example

Code Output
sprintf("%8f %8f", 3.1415, 6.283) 3.141500 6.283000
sprintf("%12f %8f", 3.1415, 6.283)     3.141500 6.283000
sprintf("%12f%8f", 3.1415, 6.283)     3.1415006.283000
sprintf("%12f%9f", 3.1415, 6.283)     3.141500 6.283000

Now let's update our code to:

  1. Set the string column to be 12 characters wide.
  2. Make the numeric columns 9 characters wide, to provide room for each number and a possible negative sign.
// Create 3x1 string array
var_names = "population" $| "poverty rate" $| "inflation";

// Create two 3x1 vectors
beta_ = { -0.65582591,
           0.94648444,
           1.7365981 };

se = {  0.19095146,
        0.25047475,
        0.11009904 };

sprintf("%12s %9f %9f", var_names, beta_, se);

will return:

  population -0.655826  0.190951
poverty rate  0.946484  0.250475
   inflation  1.736598  0.110099

This is much better. As we saw earlier, we can add any text to the format string that we'd like to see in the output. Let's add parentheses around the format specifier for the standard error column.

// Surround SE format specifier with parentheses
sprintf("%12s %9f (%9f)", var_names, beta_, se);

will return:

  population -0.655826 ( 0.190951)
poverty rate  0.946484 ( 0.250475)
   inflation  1.736598 ( 0.110099)

The parentheses in the above table make it easy for us to see the 9 character field width of the final column. If the standard error could be negative, we might want to leave it as it is to allow room for a possible minus sign. However, since we know it will be positive, let's:

  1. Decrease the field width by one character to remove the gap between the opening parenthesis and the start of the data.
  2. Add one extra space to the format string right before the opening parenthesis to keep the overall spacing between the data the same.
// Decrease field width of final column
// and add one space before the '('
sprintf("%12s %9f  (%8f)", var_names, beta_, se);

will return:

  population -0.655826  (0.190951)
poverty rate  0.946484  (0.250475)
   inflation  1.736598  (0.110099)

sprintf Precision

Our table is really starting to look good, but to match the table from the introduction, we need to reduce the number of digits to the right of the decimal place to 4.

The sprintf precision specifier is a dot or period followed by an integer. It controls the number of digits printed after the decimal point when using the floating-point f specifier character.

Code Output
sprintf("%12.2f %8.3f", 3.1415, 6.283)         3.14    6.283

After setting the precision to 4:

// Decrease precision to 4 for both numeric columns
sprintf("%12s %9.4f  (%8.4f)", var_names, beta_, se);

our output looks like this:

  population   -0.6558  (  0.1910)
poverty rate    0.9465  (  0.2505)
   inflation    1.7366  (  0.1101)

We can see that the final column displays 6 characters, but the field width is set to 8 characters. Similarly, the second column needs 7 spaces (5 digits plus a dot and a possible minus sign), but we have given it 9.

Let's fix those:

// Decrease field width for numeric columns to remove extra spaces
sprintf("%12s %7.4f  (%6.4f)", var_names, beta_, se);

We have reached our goal!

  population -0.6558  (0.1910)
poverty rate  0.9465  (0.2505)
   inflation  1.7366  (0.1101)

Conclusion

Great job! You have learned the basics you need to know to display your data with sprintf in GAUSS, including:

  • sprintf in GAUSS can print styled tables with columns of string and numeric data.
  • The styling is controlled by a format string which starts with a percent sign (%) and has 3 main components:
    • The specifier character controls whether the data is interpreted as string, decimal, integer or scientific notation.
    • The field width specifier controls the width of the column.
    • The precision specifier controls the number of digits after the decimal point when used with the floating-point, f, specifier character.

While there is much more that sprintf can do, if you can remember

"%[field width].[precision]f"

and

"%s"

you'll have a great foundation to expand on.

Leave a Reply