Example 1: Stock Portfolio
A file with comma-delimited records for a stock portfolio contains fields for
stock symbol, current price, and today’s change amount:
DIS,34.56,+1.09
T,37.05,-.42
GOOG,449.12,-11.62
To format this information into fixed-length columns so that the data can be
properly sorted and displayed, the following INREC and SORT statements may
be used:
Figure 1. Example 1, INREC Statement with PARSE
The ENDBEFR subparameters for the %1 and %2 parsed fields capture the data in the
first two fields in the input records up until the comma delimiters and
reposition the cursor after the commas, while ENDBEFR for %4 works similarly
for the last field in each record. FIXLEN sets the maximum output length for
each field. %3 is used to strip the sign off the change amount, so that the
numeric part of the amount can be right-justified. The BUILD parameter is
used to right-justify the numeric data into columns and to add spacing
between the numbers. Using INREC allows the data to be sorted by stock
symbol, producing the following output:
DIS 34.56 + 1.09
GOOG 449.12 -11.62
T 37.05 - .42
Example 2: Name and Address Data
A file has records with name and address information in a keyword
format
NAME1=GEORGE;NAME2=BUSH;ADDR1=OVAL OFFICE;ADDR2=1600 PENNSYLVANIA
AVE;CITY=WASH
NAME1=WILLIAM;MI=J;NAME2=CLINTON;ADDR1=15 OLD HOUSE LN;CITY=CHAPPAQUA;STATE=NY
NAME1=GEORGE;MI=H;NAME2=BUSH;CITY=HOUSTON;STATE=TX
PARSE may be used to search for each keyword and extract the data into
fixed-length fields in a reconstructed record. In this example, some of the
keywords in certain records may be missing. This normally would cause the
cursor to be moved to the end of the record, so that the search for the next
keyword fails. But, by using PARSE with an IFTHEN WHEN=INIT separately for
each field, this problem can be avoided because the cursor is reset to the
beginning of the record for each new PARSE.
Figure 2. Example 2, INREC Statement with IFTHEN PARSE
This produces the following output, where blanks are used for each missing
field:
GEORGE BUSH OVAL OFFICE 1600 PENNSYLVANIA AVE WASH
WILLIAM J CLINTON 15 OLD HOUSE LN CHAPPAQUA NY
GEORGE H BUSH HOUSTON TX