Data editing > Writing out data > Writing out data in a user-defined format
 
Writing out data in a user-defined format
Quick reference
To write data to the standard print file (usually called out2) in a format of your choice, type:
call qfprnt(0, $format$, variables)
format
The format in which the data is to be written and the data types of the variables used. The format string consists of optional text interspersed with references to variables in the list:
%num_posi
Print an integer variable in the next num_pos positions on the line. If the variable has a negative value the value is printed starting with a minus sign.
%num_pos.dec_plr
Print a real variable in the next num_pos positions on the line and with dec_pl decimal places. The number of print positions must allow for the required number of decimal places and a decimal point.
%num_colc
Print num_col columns starting with the column whose name or number appears in the variable list. Columns are printed as texts not punch codes; that is, multicodes are converted to letters where possible.
%numberb
Print number blank spaces.
variables
A comma-separated list of the variables to be written out. Variables must be listed in the order they are used in the format statement.
More information
write and report are both powerful statements for writing out data, but they do have limitations which you might find restrictive in some circumstances. The write statement lets you write data out to a print file, including the standard print file (usually called out2), but it always writes the data in a fixed format that you cannot change. The report statement lets you write out data and text in any format you like, but only to a report file. You cannot write to a print file with report.
The qfprnt function brings together the functionality of write and report by writing text and data to the standard print file in a format of your choice.
Example 1
call qfprnt(0,$Number of products tested is: %2i$,t1)
If the respondent tested five products this statement will appear in the standard print file as:
Number of products tests is: _5
The underscore character in front of the 5 represents a space and appears as such in the print file.
Format section
The format section of the statement consists of text to be printed exactly as it is written and references to variables whose values are to be substituted in the text at the given points. This example writes out the value of the numeric (integer) variable t1. The variable is named in the variable list section of the statement and is represented by the characters %2i in the format section.
There are three parts to the variable’s reference. The % sign signals to Quantum that it has reached a variable reference: all references start with %. The i says that the variable is an integer variable and the 2 says how many print positions to reserve for printing this variable. In the example two positions are reserved for printing the value of t1, but since the value of t1 is only 5, Quantum prints the value on the right of the reserved space and fills the remaining positions with spaces. The sample output uses an underscore to represent this space.
Here is another example using two integer variables:
call qfprnt(0,$Record %4i tested %2i products$,recnum,t1)
This produces lines of the form:
Record 1004 tested _5 products
The underscore represents a space used to pad a value to the full field width.
This qfprnt statement produces the correct results because the variables are in the same order as their references in the format section. This is your responsibility. As long as a variable has the same type as the reference in the corresponding position in the format section, Quantum prints its value at that point in the statement. So, if you write:
call qfprnt(0,$Record %4i tested %2i products$,t1,recnum)
Quantum prints:
Record ___5 tested ** products
Quantum does not increase the number of print positions to accommodate the value it needs to print. Instead, it prints asterisks. In this example, the asterisks alert you to the fact that there is something wrong with the qfprnt specification, but this would not always be so.
If Quantum needs to print a negative number, it prints the minus sign directly in front of the first digit, just as you would write it manually.
Printing real variables
To print a real variable, type:
%num_pos.dec_plr
num_pos is the number of print positions required. dec_pl is the number of decimal places. As an example, the statement:
call qfprnt(0,$%5.2r liters bought$,liters)
prints the value of the real variable called liters in a field 5 positions wide. The value is printed with two decimal places so, allowing for the decimal point, the maximum value that can be printed in 99.99:
15.27 liters bought
9.01 liters bought
Quantum can also print the text values of a column, a field of columns or a data variable. This means that Quantum converts multicodes to letters or other keyboard characters before printing them. Multicodes that do not correspond to letters or characters are printed as asterisks. For example, the multicode ‘&1’ translates into the letter A and would be printed as such; the multicode ‘&123’ is simply as collection of codes and would therefore be printed as an asterisk.
Printing columns
To print single columns, type:
%numberc
in the format section, where number is the number of print positions required, and the name of a single column in the corresponding position in the variable list. Quantum will then print number columns starting at the named column. For example:
call qfprnt(0,$Record %4c tested %2i products$,c1,t1)
might produce:
Record 1234 tested 5 products
Now suppose that the data is:
----+---2
9462&5736
5 1 8
9
The statement:
call qfprnt(0,$Columns 11 to 20 are %10c: $,c11)
reports the following:
Columns 11 to 20 are: 9*62A5*36
Printing blank strings
To print strings of blanks, type:
%numberb
number is the number of blanks you want. This is useful if you want to indent lines or print values in columns.
See also
Writing out data