• No results found

Default print parameters for write statements

In document Quantum Vol1 (Page 82-92)

Writing out parts of records

Often you will not want to write out the whole record, especially if it contains several cards.

Therefore Quantum allows you to include a field specification in a write statement to print only selected portions of an incorrect record. For example:

if (c110’2’.and.c119’2’) write c(110,120) $Married woman$

checks that columns 110 and 119 both contain a 2, and if so prints out columns 110 to 120 in the print file, followed by the text Married woman. If you are writing out fewer than ten columns, Quantum does not print a ruler above the codes.

If you are dealing with multi-card records, you may prefer to use this form of write to print only the card containing the error, rather than all cards in the record. If we take our previous example where we were checking the contents of column 308:

if (c308n’1/5’) write $c308 incorrect$

prints all three cards in the record, whereas:

if (c308n’1/5’) write c(301,380) $C308 incorrect$

prints only card 3.

To write selected parts of a record to a particular file the notation is:

write filename c(m,n) [$text$]

The write statement can only write out information from the C array.

7.2 Data files

Quick Reference

To write records or fields to a data file, type:

write file_name [c(start_col, end_col)]

write may also be used to copy records to a data file. This is useful if you want to separate a particular card type from the rest of the data, or if you want to correct errors and save the corrected data in a new file for later tabulation.

To write records to a data file the command is:

write filename

to write the whole record to the named file, or write filename c(m,n)

to write columns m to n only.

If you use write in a levels job to write data to a new data file, the statement write datafile at any level will write out data for that level only. Additionally, if the write statement is inside an if clause, or a return statement is encountered, then only relevant data is written for that level. To write out data for all levels, you will need one write statement per level.

In all cases, records are written in the state they are when the write is executed, and all cards read in with the current read are copied; that is, all cards for which thisread is true. For instance, if thisread1, thisread2 and thisread3 are true, Quantum will write out cards 1, 2 and 3. To prevent any of these cards being written, you may set the appropriate variable to false (zero); therefore to print only card 1 of our three cards, we would write:

thisread2=0; thisread3=0 write newdat

Any number of writes to data files are allowed in the edit, and each one may write to a different file.

Records written by write are normally as long as the record length defined with reclen on the struct statement. You may change this with len= on the filedef statement. The exception is where records end with blank columns. In this case Quantum ignores the blank columns. If you want to create a data file of fixed length records, and your data is single coded, you can use the reportn statement.

If your data is multicoded you can convert it to single coded first by using the explode statement.

For further information about explode, see ‘Converting multicoded data to single-coded data’

in chapter 13, ‘Using subroutines in the edit’.

If your data is multicoded and you need to preserve the multicodes, the only way of writing out fixed length records if the data currently has trailing blank columns is to insert a dummy code in the last column of those records.

Creating new cards

New cards can be created by copying information into spare columns of the C array. To save these as part of a new data file you will have to give each new card the same respondent serial number as the rest of the data in the array and a card type which may or may not be unique. In the example below, we are moving some information from card 1 of a 2-card data set into a new card 3. The comments explain what each statement is doing.

/* Copy the data into the new card c(310,341)=c(148,179)

/* Delete it from its original place c(148,179)=$ $

/* Give it a serial number and card type c(301,304)=c(101,104); c380’3’

/* Set thisread true for card 3 thisread3=1

/* Define pfil as a data file filedef pfil data

/* Copy cards 1, 2 and 3 to pfil write pfil

7.3 Writing to a report file

Quick Reference

To write information to a report file, type:

report[n] file_name variable_names

where variable_names is a comma-separated list of the variables and texts to print.

Use reportn rather than just report to start a new line each time the statement is executed.

A report file is a special type of print file in which you can print out records, fields or variables in the format of your choice. To write information in a report file, use the report statement, as follows:

report filename parameters

where filename is the name of the file to be written to, and parameters define exactly what is to be written.

Lines in a report may be up to 1024 characters long. Report does not start a new line automatically at the end of each write, but you may tell it to do so by following the keyword report with the letter n:

reportn filename parameters

In both cases, the named file must be identified as a report file using a filedef statement, as described in section 7.4, ‘Defining the file type’.

The parameter list defines what is to be printed in the report file. It may contain variables, texts, and special characters representing tabs and spaces.

Data variables

Quick Reference

To print the contents of a data variable, type:

var_name or var_name(start,end)

To print the contents of a field, evaluated as an integer right-justified in a field of a given width, type:

var_name:field_width

To print a the contents of every column in a field, even if they are multicoded or blank, type:

start:field_width

where start is the first position in the field. You may also use this notation to print fields whose contents evaluate to a value greater than the maximum integer value Quantum can deal with.

To print the contents of a data variable, type the variable’s name.

All data variables that are single coded are printed using as many positions as there are columns in the variable. For example, if the data is:

----+----4 511 538253 2

&

the statement:

report rfile c31,c35,c40

prints the contents of columns 31, 35 and 40 one after the other, as follows:

553

The statement:

report rfile c(35,40)

prints the contents of columns 35 to 40:

538253

In both the examples the last column of the field has contained a code. If the last column or columns of a field are blank, Quantum omits those columns when printing the contents of the field. (You can get round this by entering the field specification as start:field_width as described later in this section.)

A single data variable that is blank is printed as such, while a single data variable that is multicoded is printed as an asterisk. The statement:

report rfile c35, c34, c33, c32 creates the line:

5 *1

If a variable refers to a string that contains multicoded or blank columns, Quantum ignores the multicodes and blanks and evaluates the contents of the remaining columns as an integer. For example, using the data shown above, the statement:

report rfile c(31,40)

prints a line containing the value 51538253. The value starts in the first print position available.

If the field you wish to print is very long, its contents may produce an incorrect value when evaluated as an integer (the maximum integer value which Quantum can deal with is 1,073,741,824). You can get round this by specifying the first column and the field width as described below.

If you want to see all columns in a field which contains blanks or multicodes, or you need to have the correct evaluation of a long field, you will need to deal with each column in the field separately.

You could type each column number separately, but it is quicker just to specify the start column and the total number of columns you want to print starting at that column.

The format for this type of reference is:

start:field_width

For instance, to print columns 31 to 40 you would type:

report rfil c31:10

The output from this command would be 51* 538253, the same as if you had typed each column number separately. As before, the data is printed starting in the first print position available.

You can use this alternative notation with field specifications too. In this instance Quantum will evaluate the contents of the field as an integer and will print the result right-justified in a field of the given width. If you type, for example:

report rfil c(31,40):10

Quantum will print the value 51538253 in positions 3 to 10 of a ten-position field. The first two positions will be blank.

This notation is also useful if you need to create data files with fixed length records, and some records end with blank columns. Writing records to a data file preserves multicodes but ignores trailing blank columns. Writing to a report file allows you to create a single-coded data file with fixed length records. If your data is multicoded you will need to convert it to single-coded form before writing it out. You can do this by ‘exploding’ any multicodes into a field of single codes.

You use the explode statement for this.

For information on how to use explode, see ‘Converting multicoded data to single-coded data’

in chapter 13, ‘Using subroutines in the edit’.

Once your data is in single-coded form you can then write the whole record out to a report file using a reportn statement as follows:

reportn repdata c101:80 reportn repdata c201:80

Integer variables

Quick Reference

To print the contents of an integer variable, type:

var_name[:field_width]

If the report statement names a variable by itself, Quantum prints the variable’s value starting in the first print position available. If the specification includes a field width, Quantum prints the variable’s value right-justified in a field of the given width. Any extra columns on the left of the field width are shown as blanks.

To print the value of an integer variable, type:

var_name[:field_width]

If you type the variable name by itself, without a field width, Quantum prints it left-justified starting in the first available position on the line. If you would prefer values to be printed right-justified, follow the variable name with a colon and a field width. Quantum will then print all values for that variable right-justified in a field of the width you have given. For example:

report rfile codenums:5

prints the values of the variable called codenums right-justified in a field five positions wide.

Values that are shorter than five characters are padded on the left with blanks.

Real variables

Quick Reference

To print the value of a real variable, type:

var_name[:field_width.dec_places]

where field_width is the width of the field in which the values are to be printed (values are right-justified and padded on the left with blanks if necessary) and dec_places is the number of decimal places to be shown for each number. If you omit these parameters, Quantum prints the values starting in the first available print position and with six decimal places.

To print the value of a real variable, type:

var_name[:field_width.dec_places]

If you type the variable name by itself, without a field width and a number of decimal places, Quantum will print the variable’s value with six decimal places and starting in the first print position available.

You can control the layout by defining a field width and the number of decimal places required.

For example, by typing:

report rfil cost:6.2

you can create a neat column of figures all with two decimal places and all right-justified in a field six characters wide.

Text and white space

Quick Reference To print text, type:

$text$

To create blank space on a line, type:

[number]x

to leave the given number of spaces (the default is one), or:

print_post

to tab to the given position on the line.

Most reports require some sort of text or spacing on the line, either on the same line as the values or on lines by themselves to create titles, column headings, and the like.

To print text on a report, type:

$text$

The text may contain spaces.

To print spaces between the values on a line, you can either use spaces or tabs. To print a given number of spaces between one value and the next, type:

[number]x

where number is the number of spaces required. The default is one space.

If you are producing tabular or columnar output you’ll probably find tabs are more useful for creating blank space since they allow you to skip to a particular print position on the line. For example, typing:

25t

takes you directly to position 25 on the line, regardless of the current print position. Compare this with 25x which moves you 25 positions on from your current position.

Examples

Here are some examples of report statements:

reportn summary 20t,$Bought Brand A$,1x,brda:3,1x,$times$

prints a report of the form:

Bought Brand A 5 times Bought Brand A 9 times Bought Brand A 13 times

in a file called summary. Printing starts in position (column) 20 because we started the parameter list with the keyword 20t. The variable brda is an integer variable whose value is to be right-justified in a field three columns wide. Notice also how we have inserted spaces between the texts and the value of brda.

The statements:

/* only print title if this is the first record in the data file if (.not. rchk)

+reportn yogurt 30t,$Serial Numbers for Yogurt Buyers$

if (c119’1’) reportn yogurt c(1,4) .

rchk = 1

produce a report showing the serial numbers of all respondents who buy yogurt. As you can see, we have given our report a title.

As a final example, let’s look at the difference between printing a field of columns all in one go and printing them one at a time. If our data is:

+----4----+

18 036 & / 7 the statement:

reportn test $c(37,43) is $,c(37,43) will produce the line:

c(37,43) is 106

However, if we deal with each column separately:

reportn test $c(37,43) is $,c37,c38,c39,c40,c41,c42,c43 will report that:

c(37,43) is 1* 0*6

You cannot write information to the standard print file (usually called out2) using report. To do this use the function qfprnt.

In document Quantum Vol1 (Page 82-92)