Unusual Functions and Commands

advertisement
Unusual Command and
Function Usage
DEFINE FIELD
Scenario: You have a data file.
The file is fixed length, you can
create fields based upon the
location of values from the
data file alone.
DEFINE FIELD
DEFINE FIELD field_name data_type
start_position field_length
<decimals|date_format> <AS
"alternate_column_title"> <WIDTH> <PIC
"format">
Example:
DEFINE FIELD Transaction_date DATE 6 10
MM/DD/YYYY
Incomplete Fields
You have data that you need to complete:
Original Data
Desired Data
Last Name First Name
User Name First Name
Smith
John
Smith
John
Sue
Smith
Sue
Max
Smith
Max
Jones
Bridget
Jones
Bridget
Doe
Jane
Doe
Jane
Frank
Doe
Frank
DEFINE FIELD New_Field COMP
STATIC IF Last_name <> “”
Last_name
DEFINE FIELD: Control Totals
Another command often under utilized that
can be used for data
integrity/completeness is the Control Total
parameter on Define Fields. Control Total
will calculate the value of the total of the
field and preserve that value in the ACL
log and table history.
Control Total Cont:
DEFINE FIELD F_amt COMPUTED
CT
AMT
MATCH COMMAND
Typical use:
MATCH(fieldname, “Text1”,
“Text2”,”Text3”, … “textX”)
But what if you know what value you are
looking for, you just don’t know what field it
will occur in?
Match cont
In other words, you are looking for the value
“Text1” but it can occur in Field1, Field2,
or Field3. How can you find it?
Solution:
MATCH(“Text1”, Field1, Field2, Field3)
Scenario
You have a field that is composed of
numerics and alpha characters. The
numeric characters start the field. You
want to keep just the numeric values that
start the field:
12345ABCDE  12345
678FGH12
 678
9999999DSFG  9999999
Solution:
DEFINE FIELD newfield COMPUTED
CLEAN(UPPER(oldfield),
“ABCDEFHIJKLMNOPQRSTUVWXYZ”)
12345ABCDE  12345
678FGH
 678
9999999DSFG  9999999
Second half:
Same field, but you want to keep everything
after the alpha characters begin:
12345ABCDE  ABCDE
678FGH12
 FGH12
9999999DSFG  DSFG
SOLUTION
SUB(oldfield,LEN(CLEAN(UPPER(oldfield),”
ABCDEFGHIJKLMNOPQRSTUVWXYZ”+
1,50)
Scenario
You have a variable that contains an
unknown number of values. Each value is
separated by a bar delimiter (|). You want
to perform a code that repeats the same
code for each value in the variable.
To do this, you intend to use a GROUP
IF/DO WHILE/LOOP WHILE command,
how can you insure that you are repeating
the code the appropriate number of times?
Solution:
V_cntr = OCCURS(fieldname,”|”) + 1
V_cnt = 1
DO Subscript IF v_cnt <= v_cntr
Within ths subscript, you would then use:
SPLIT(fieldname,”|”,V_cnt)
v_cnt = v_cnt +1
AT
The AT() function is a good alternative to the
SPLIT() function. You can use it in a
similar manner. But you can also use it to
make counts or determine sizes.
Take Command Example
Item_No, Description, Qty_on_Hand, Qty_on_Order, Acquire_Date, Cost_Per_Unit
0289675, Lineman’s Pliers, 800, 200, 05/13/2007, 32.99
0289643, Wire Stripper, 300, 100, 05/15/2007, 21.99
0374582, Wire Cutter, 8 Gauge, 250, 50, 05/20/2007, 39.99
0337583, Wire Cutter, 12 Gauge, 200, 100, 05/24/2007, 34.99
“However, since we know that there should
be 6 fields and also that if there is an error, it
occurs in the second field, we can script the
input and account for the deficiency.”
Load and
1 IMPORT DELIMITED TO Interim "Interim.fil" FROM %import_file% 0
SEPARATOR "" QUALIFIER NONE CONSECUTIVE STARTLINE 1 FIELD
"data_line" C AT 1 DEC 0 WID 55 PIC "" AS ""
2 DEFINE FIELD item_num COMPUTED SUBSTR( SPLIT( data_line, ",", 1),
1, 6)
3 DEFINE FIELD Qty_OH COMPUTED VALUE( REVERSE( SPLIT(
REVERSE(data_line),",", 4)), 0)
4 DEFINE FIELD Qty_OO COMPUTED VALUE( REVERSE( SPLIT(
REVERSE(data_line), ",", 3)), 0)
5 DEFINE FIELD Acq_Dt COMPUTED CTOD( REVERSE( SPLIT(
REVERSE(data_line),",", 2)), "MM/DD/YYYY")
6 DEFINE FIELD Cost_Per_Unit COMPUTED VALUE( REVERSE( SPLIT(
REVERSE(data_line), ",", 1)), 2)
1 COMMENT Description Field Definition Section
2 DEFINE FIELD description COMPUTED
3 COMMENT Define Description field based on number of comma
occurrences
4 SUBSTR( SPLIT( data_line, ",", 2) + " " + SPLIT( data_line, ",", 3), 1,
25) IF OCCURS( data_line, ",") = 6
5 SUBSTR( SPLIT( data_line, ",", 2) + ", " + SPLIT( data_line, ",", 3) +
", " + SPLIT( data_line, ",", 4), 1, 25) IF OCCURS( data_line, ",") >= 7
6 SUBSTR( SPLIT( data_line, ",", 2), 1, 25)
Warning:
“It is important to note that if the line
possesses 8 or more commas, the last
section of the description field will be
truncated. If the line possesses 9 or more
commas, the last 2 sections of the
description field will be truncated.”
8. DEFINE FIELD qty_OH COMPUTED
SPLIT(data_line,",",OCCURS(",")-2)
9. DEFINE FIELD qty_OO COMPUTED
SPLIT(data_line,",",OCCURS(",")-1)
10. DEFINE FIELD Acq_dt COMPUTED
SPLIT(data_line,",",OCCURS(","))
11. DEFINE FIELD Cost_per_unit COMPUTED
SPLIT(data_line,",",OCCURS(",")+1)
13. DEFINE FIELD Description COMPUTED SUB(data_line, AT(2,
",", data_line)+1, AT(OCCURS(",")-3, ",", data_line)-AT(2, ",",
data_line)-2)
Download