7/30/2019 Rpg Preset
1/144
7/30/2019 Rpg Preset
2/144
RPG/400
Session 1
RPG Programming Language
7/30/2019 Rpg Preset
3/144
INTRODUCTION
RPG is an abbreviation for Report Program Generator.It is a high level languagein which the programmer codes the specifications.
RPG code is written on a variety of specification forms, each with a specific setof functions.
Many of the entries which make up a specification type are position-dependent.
Developed by IBM in 1960s.
Each entry must start in a specific position depending on the type of entry andthe type of specification.
There are seven types of RPG IV specifications.
Each specification type is optional.
Specifications must be entered into your source program in the order HFDICO.
7/30/2019 Rpg Preset
4/144
FEATURES OF RPG Structured language
It helps us in approaching a modular program design,and also facilitates us
with the sequence,selective and iterative constants.
Strong at file operationRPG does not impose any limitations in accessing the data records unlike
the other programming languages.The data records can be accessed in anydirection,sequentially or randomly with out need to open and close the files.
Strong interactive programming languageRPG in combination with Screen Design Aid (SDA) forms a powerful
interactive programming language.the screens designed through SDA can be easily
accessed from an RPG program.
Sub filesThis IS excellent feature of RPG.sub files are special kind of screen files
using which we can display multiple records onto a screen with a minimal
programming effect.
7/30/2019 Rpg Preset
5/144
Provision of Arrays:Arrays is an useful concept,which is provided by almost all the high level
languages and also it makes the code looks very shorter.
Generation of Complex Reports:RPG in combination with RLU(Report Layout Utility)helps us in generating
formatted and complex reports.
Interface with CL language :we can establish data communication among a CL language and an RPG
program.
7/30/2019 Rpg Preset
6/144
RPG Figurative constants:
The letter begin with an asterisk with an few exceptions.
*LOVAL
*HIVAL
*BLANKS
*ZEROS
*LIKE
7/30/2019 Rpg Preset
7/144
Date group reserved words
*UDATE
*UDAY
*UMONTH
*UYEAR
UDATE is a six-digit numeric,and others are each two-digit
numeric fields.the format of UDATE is determined by the system
value,which has been set at installation and may be ddmmyy,mmddyy, oryymmdd.
It may be overridden by an entry in column 19 of H spec.
7/30/2019 Rpg Preset
8/144
RPG/400 SPECIFICATIONS
RPG source statements consists of 80-character card images having line numbersin the first few columns of each source statements.
Any statement with an asterisk*in column 7 is a comment.
Every line having a form type in column 6.
Specification types:
1. Control specificationsprovide the compiler with information about generating andrunning programs, such as the program name, date format, and use of alternatecollating sequence or file translation.
2. File description specificationsdescribe all the files that your program uses.
7/30/2019 Rpg Preset
9/144
3. Definition specificationsdescribe the data used by the program.
4. Procedure specificationsdescribe prototyped procedures that are specifiedafter the main source section, otherwise known as subprocedures
5. Calculation specificationsdescribe the calculations done on the data and theorder of the calculations. Calculation specifications also control certain input andoutput operations.
6. Output specificationsdescribe the output records and fields used by theprogram.
7/30/2019 Rpg Preset
10/144
File Specifications
RPG/400
Session 2
7/30/2019 Rpg Preset
11/144
File Description specification
Each File used in a program must be described in FSPEC. One file is described per entry.
Only one primary file can be specified. The presence of a primary file is not required.
Only one record-address file.
A maximum of eight PRINTER files.
There is no limit for the maximum number of files allowed.
7/30/2019 Rpg Preset
12/144
File Description Specification Statement
Position Description6 F
7-80 non-commentary part
742 fixed-format entries
448081100 Comments
Key word entries
7/30/2019 Rpg Preset
13/144
Columns Description
1-5 Sequence number
6 F
7-16 File Name
17 File Type:
C Combined
I Input, O Output, U Update
18 File Designation :
Blank Output
F Full procedural
P Primary
R Record Address
S SecondaryT Array or Table
19 End of file:
Blank allow LR before all records are processed.
E all records must process before LR
7/30/2019 Rpg Preset
14/144
Columns Description
20 File addition
Blank Do not add records
A Allow record Adds
21 Match field sequence
Blank or A Ascending
D Descending
22 File Format:
E Externally Described
F Program Described
23-27 Record Length
28 Limits processing :
Blank : Sequential or random processing
L Limits processing
29-33 Length of key or record address
7/30/2019 Rpg Preset
15/144
Columns Description
34 Record Address type
Blank Non keyed processingA - Character keys
D - Date Keys
G - Graphic keys
K - Externally described key
P - Packed key
T - Time Keys
Z - Time Stamp Keys
35 File Organization
B Non Keyed , Program described
I - indexed
T Record address
7/30/2019 Rpg Preset
16/144
Columns Description
36-42 DeviceDisk - Disk file
Printer - Printer File
SEQ - Sequentially Organized
SPECIAL - Special device
WORKSTN - Workstation file
43 (reserved)
44-80 File Keywords
81-100 Comments
7/30/2019 Rpg Preset
17/144
FILE READ
0001.00 FEMPMASTER IF E K DISK
0002.00 FDSPLYFILE CF E WORKSTN
0002.01 C READ EMPRECFMT 10
0005.00 C DOW *IN10=*OFF
0006.00 C EXFMT READING
0007.00 C READ EMPRECFMT 10
0008.00 C ENDDO
0009.00 C SETON LR
File File End of File
Filename Type Designation File Addition Sequence
EMPMASTER I F
File Limits Record
Format Processing Address Type Device
E K DISK
7/30/2019 Rpg Preset
18/144
Calculation Specifications
RPG/400
Session 3
7/30/2019 Rpg Preset
19/144
In this specification we code variable declaration and the executable statements.
It consists of essentially a verb and three operands known an FACTOR-1,
FACTOR-2, RESULT
Operation codes, entered on the calculation specifications, indicate whatoperations will be done.
For example, if you want to read a new record, you could use the READ operation
code.
C-SPEC
7/30/2019 Rpg Preset
20/144
Columns Description
1-5 Sequence number
6 C
7-8 Control Level : Blank,L0,L1-L9,LR,SR,AN,OR
9-11 Indicators : Blank,01-99,KA-KN,KP-KY,L1-L9,LR,MR,H!-H9,RT,U1-U8,OA-
OG,OV
12-25 Factor 1
26-35 Operation code and Extender
Layout
7/30/2019 Rpg Preset
21/144
Columns Description
36-49 or
36-80
Factor 2 or Extended factor 2
50-63 Result Field
64-68 Result Field Length
69-70 Result Field Decimal Positions
71-76 Resulting Indicators (High/Low/Equal)
77-80 (Blank)
81-100 Comments
7/30/2019 Rpg Preset
22/144
Indicators
An indicator is a one-byte character field that is either set on (1) or off (0).
It is Generally used to indicate the result of an operation or to condition (control)the processing of an operation. Indicators are like switches in the flow of theprogram logic.
They determine the path the program will take during processing, depending on
how they are set or used.
Indicators can be defined as variables on the definition specifications. You can alsouse RPG IV indicators, which are defined either by an entry on a specification or bythe RPG IV program itself.
How to set OFF and ON the indicator
In RPG400 we have opcodes SETON SETOFF or Move
0 Move 1 For ILE RPG we have EVAL *IN = *ON or *OFF Or we can use the samefunctions as above
7/30/2019 Rpg Preset
23/144
Indicators are of 2 types
General indicators and special indicators
General Indicators are 0 99
Special Indicators:
1. Halt indicators H1 H9. These are used to store data in punch cards.
2. External indicators U1 to U8. Used to pass parameters 3. First Page 1P - used for logic cycle
4. Matching Indicator MR - used for logic cycle
5. Level Break L0 L9, LR Last record ( used for logic cycle)
6. Return Indicator RT used as return function
7. Overflow indicator OA to OG, OV used for printer files
8. Command keys (or) Function keys KA to KN, KP to KY command level
keys F1 to F24
7/30/2019 Rpg Preset
24/144
7/30/2019 Rpg Preset
25/144
1. Arithmetic operations
ADD
SUB
MULT
DIV
MVR
SQRT
Z-ADD
Z-SUB
2. Move operations
MOVE
MOVEL
DEFN
Types of OPCODES
7/30/2019 Rpg Preset
26/144
3. Date/time operations
TIME (Time of Day)4. Structured programming operations
IFxx (If)
SELEC (Begin a Select Group)
WHxx (When True Then Select)
DO (Do)ENDDO
DOUxx (Do Until)ENDDO
DOWxx (Do While)ENDDO
ITER (Iterate)
LEAVE (Leave a Do Group)
7/30/2019 Rpg Preset
27/144
5. Branching operations
TAG (Tag)
GOTO (Go To)
CABxx (Compare and Branch)
6. Call operations
BEGSR (Beginning of Subroutine)
ENDSR (End of Subroutine)
EXSR (execute Subroutine)
CASxx (Conditionally Invoke Subroutine)
7/30/2019 Rpg Preset
28/144
CALL (Call a Program)
PARM (Identify Parameters)
PLIST (Identify a Parameter List)
7. File operations
OPEN (Open File for Processing)
CLOSE (Close Files)
READ (Read a Record)
WRITE (Create New Records)
READC (Read Next Changed Record)
7/30/2019 Rpg Preset
29/144
READE (Read Equal Key)
READP (Read Prior Record)
REDPE (Read Prior Equal)
SETGT (Set Greater Than)
SETLL (Set Lower Limit)
KFLD (Define Parts of a Key)
KLIST (Define a Composite Key)
CHAIN (Random Retrieval from a File)
7/30/2019 Rpg Preset
30/144
UPDAT (Modify Existing Record)
DELET (Delete Record)
COMIT (Commit)
ROLBK (Roll Back)
UNLCK (Unlock a Data Area or Release a Record)
8. Data area operations
IN (Retrieve a Data Area)
OUT (Write a Data Area)
7/30/2019 Rpg Preset
31/144
9. Array operations
MOVEA (Move Array)
XFOOT (Summing the Elements of an Array)
LOKUP (Look Up a Table or Array Element)
SORTA (Sort an Array)
7/30/2019 Rpg Preset
32/144
SCAN (Scan String)
CHECK (Check Characters)
SUBST (Sub string)
XLATE (Translate)
CAT (Concatenate Two Strings)
10. String operations
7/30/2019 Rpg Preset
33/144
ARITHMATIC OPERATIONSADD
The add verb is used to find the sum of numeric values.it has two formats:
Format1:
CODE Factor 1 Factor 2 Result Field INDICATORS
ADD NUM1 NUM2 RNUM HI LO EQ
This statement is equivalent to saying RNUM=NUM1+NUM2
7/30/2019 Rpg Preset
34/144
CODE Factor 1 Factor 2 Result Field INDICATORS
ADD NUM2 RNUM HI LO EQ
Format : 2
If factor1 is blank,factor2 is added to result and sum placed in result field.
This statement is equivalent to saying RNUM=RNUM+NUM2
7/30/2019 Rpg Preset
35/144
SUB
The SUB verb is with numeric values for subtract operation.
MULT
The MULT verb is used to find the product of two numbers.
DIV
The DIV verb is used for division operation with numeric values.
MVR
The MVR operation moves the remainder from the previous DIV operation to a
separate field named in the result field.
7/30/2019 Rpg Preset
36/144
SQRT
The SQRT operation derives the square root of the field named in the result
field.
Z-ADD
The Z-ADD stands for zero add .zero is added to the factor 2 and the sum isplaced in the result.this is basically used to declare,initialize or move a value
into the numeric filed.
Z-SUB
The Z-SUB stands for zero subtract .zero is factor 2 is subtracted from zero
and the difference is placed in the result.this is basically used to move a
negative value into the filed.
7/30/2019 Rpg Preset
37/144
program for Arithmetic operation
C MOVE 9 A 2 0
C MOVE 7 B 2 0
C MOVE *ZEROS C 3 0
C MOVE *ZEROS D 2 0
** A = A + B
C* ADD B A
C* A DSPLY**
C A ADD B C
C C DSPLY
**
C A SUB B C
C C DSPLY
**
C A MULT B C
C C DSPLY
**
7/30/2019 Rpg Preset
38/144
C A DIV B C
C MVR D
C C DSPLY
C D DSPLY
C SQRT A C
C C DSPLY
**
C Z-ADD 2 E 2 0
C E DSPLY
**
C Z-SUB 2 F 2 0
C F DSPLY
C SETON LR
O/P
DSPLY 16 ,DSPLY 2 ,DSPLY 63 ,DSPLY 1 ,DSPLY 2 ,DSPLY 3
DSPLY 2 ,DSPLY 2-
7/30/2019 Rpg Preset
39/144
Move operations
MOVE
MOVE is used to move data into a field.the value specified in factor1 will bemoved into the filed specified in result field.
MOVEL
MOVEL (move left) is similar to MOVE with the only difference that the factor 2will be moved in to but left is justified.
DEFN
Define field based on the attributes (data type,length and decimal positions)of
another field.
Define field as a data area.
7/30/2019 Rpg Preset
40/144
0001.00 C MOVE 2345678 NUM1 5 0
0002.00 C NUM1 DSPLY
0002.01 **
0002.02 C MOVEL 1234637 NUM1 5 0
0002.03 C NUM1 DSPLY
0002.04 **
0004.03 C MOVEL 'ABCDE' RES 3
0004.04 C RES DSPLY
0004.05 **
0004.06 C MOVE 'ABCDE' RES 3
0004.07 C RES DSPLY
0004.08 C SETON LR
program for move operation
7/30/2019 Rpg Preset
41/144
O/p
DSPLY 45678
DSPLY 12346DSPLY ABC
DSPLY CDE
Structured programming operations
IFxx
IF operation is used for conditional execution of statements.Each IF have a
corresponding ENDIF or END statement.
The statements placed between the IF and ENDIF will be executed only when
the condition satisfied The condition may also be a compound condition supplemented by ANDxx or
ORxx
ELSE state can also be coded between IF and ENDIF,So that if the condition is
not satisfied then the statements between ELSE and ENDIF can be executed.
7/30/2019 Rpg Preset
42/144
NOTE:XX Denotes a relational operator which could any one of the following
EQ Equal to
NE Not equal to
GT Greater than
LT Less than
GE Greater than or equal to
LE Less than or equal to
7/30/2019 Rpg Preset
43/144
Example 1
Calculation of Bonus AND PERKS if basic greater than or equalto 3000
0002.00 C Z-ADD3000 BASIC 40
0003.00 C BASIC IFGE 3000
0004.00 C Z-ADD4000 BONUS 40
0005.00 C Z-ADD3000 PERKS 40
0005.02 C BONUS DSPLY
0005.03 C PERKS DSPLY
0005.04 C ENDIF
0007.00 C SETON LR
o/p
DSPLY 4000
DSPLY 3000
7/30/2019 Rpg Preset
44/144
Example :2
A clerk with salary more than 2500 or a manager will get abonus of 4000 and perks of 3000.otherwise bonus would be2000 and perks would be 12000000.02 C MOVEL 'CLERK' DESIGN 7
0000.03 C MOVEL 'MANAGER' DESIGN 7
0000.04 C Z-ADD 2500 BASIC 4 0
0001.00 C DESIGN IFEQ 'CLERK'0001.01 C BASIC ANDGT 2500
0001.02 C DESIGN OREQ 'MANAGER'
0001.03 C Z-ADD 4000 BONUS 4 0
0001.04 C Z-ADD 3000 PERKS 4 0
0001.07 C ELSE
0002.00 C Z-ADD 2000 BONUS 4 0
0002.01 C Z-ADD 1200 PERKS 4 0
0005.01 C ENDIF
7/30/2019 Rpg Preset
45/144
NOTE:
Only change manager to manager1 you get an output as 2000,1200
SELEC : when we have multiple nested IFs the code becomes complex and we will
have to take care of matching the IFs with there corresponding ENDIFs.
SELECENDSL can be used as an alternative in such a case.
Here we have multiple WHXX statements between SELEC and ENDSL.if
any of the WHXX condition is satisfied then the statements under it will be
executed.if none of the condition satisfy then the statements under OTHER
will be executed.
0005.04 C BONUS DSPLY
0005.05 C PERKS DSPLY
0006.00 C SETON LRO/p
DSPLY 4000
DSPLY 3000
7/30/2019 Rpg Preset
46/144
DO ..ENDDO :A DO statement can be used to repeat the code enclosed between do and ENDDO,a
specific number of times.
SYNTAX: FACTOR1 OPCODE FACTOR2
SELEC
WHLT
------
OTHER
------
ENDSL
Example:
DISPLAY ALL ODD NUMBERSS BETWEEN 1 TO 10
0002.00 C 1 DO 10 NUM 5 00003.00 C NUM DSPLY
0004.00 C ENDDO 2
0005.00 C SETON LR
O/P
1,3,5,7,9
7/30/2019 Rpg Preset
47/144
DOUXX ..ENDDO :DOUXX(do until loop) this loop will be executed at least once irrespective Of whether
the condition is satisfied initially or not.BECAUSE the condition is post-tested .
Example:
INITIALIZATION AND INCREMENTATION HAS TO BE EXPLICITLYDONE
0001.00 C Z-ADD 1 NUM 5 0
0002.00 C NUM DOUGT 10
0003.00 C NUM DSPLY
0004.00 C ADD 2 NUM
0005.00 C ENDDO0006.00 C SETON LR
O/P
1,3,5,7,9
7/30/2019 Rpg Preset
48/144
DOWXX ..ENDDO :DOWXX(do while loop) is similar to DOU loop except that,the loop continues execution
as long as the condition is true and terminates once it becomes false.Also the conditionis per-tested.
Example:
DISPLAY ALL ODD NUMBERSS BETWEEN 1 TO 10
0002.00 C Z-ADD 1 BASIC 2 0
0002.01 C BASIC DOWLE 10
0002.02 C BASIC DSPLY
0002.03 C ADD 2 BASIC
0002.04 C ENDDO0003.00 C SETON LR
O/P
1,3,5,7,9
7/30/2019 Rpg Preset
49/144
LEAVE :LEAVE statement can only be given inside a loop.it abruptly terminates the loop
execution and transfers the control to the next statement after ENDDO.
ITER :ITER statement can only be given inside any of the loops.it is used to transfer the
control to the beginning of the loop to continue execution.
Example:DISPLAY ALL ODD NUMBERSS EXCEPT 5 ,BETWEEN 1 TO 10
0001.00 C Z-ADD 1 NUM 5 0
0002.00 C NUM DOWLE 10
0003.00 C NUM IFEQ 5
0004.00 C ADD 2 NUM
0005.00 C ITER
0005.01 C* LEAVE
0006.00 C ENDIF
7/30/2019 Rpg Preset
50/144
0007.00 C NUM DSPLY
0008.00 C ADD 2 NUM
0009.00 C ENDDO
0010.00 C SETON LR
O/P
1
3
7
9
NOTE:
In the above example if leave is coded in place of ITER the the loop execution
terminates once the value of num is equal to 5.hence only 1 and 3 will get displayed.
7/30/2019 Rpg Preset
51/144
Branching operations
TAG & GOTO
TAG is used in accordance with GOTO in a program.Using GOTO we cantransfer the control from one place to another with in a routine.
The place where the control needs to be transferred should be labeled(named)using a TAG statement.
NOTE:
While using TAG,only factor1 is coded which contains the label name.
GOTO statement is coded with the name of the label in factor 2.
7/30/2019 Rpg Preset
52/144
Example:
DISPLAY NUMBERS FROM 10 TO 1
0002.00 C Z-ADD10 NUM 50
0002.01 C CBSI TAG
0003.00 C NUM DSPLY
0004.00 C SUB 1 NUM
0005.00 C NUM IFGE 1
0006.00 C GOTO CBSI0006.01 C* NUM CABGE1 CBSI
0007.00 C ENDIF
0008.00 C SETON LR
O/P
10,9,8,7,6,5,4,3,2,1
7/30/2019 Rpg Preset
53/144
CABXX :CABXX(COMPARE AND BRANCH)command is simply a combination of IF & GOTO
statement.Example:
DISPLAY NUMBERS FROM 10 TO 1
0002.00 C Z-ADD10 NUM 50
0002.01 C CBSI TAG
0003.00 C NUM DSPLY
0004.00 C SUB 1 NUM
0006.01 C NUM CABGE1 CBSI
0008.00 C SETON LR
O/P
10,9,8,7,6,5,4,3,2,1
7/30/2019 Rpg Preset
54/144
The time operation access the system time of day and,if specified the
system date at any time during program processing.the system time is
based on 24-hour clock.
Result : Must contain either at 6,12 or 14 digit numeric field(no decimal
positions)
If six-digit numeric field :Time of the day only
If 12-digit numeric field :Time of the day and system date with out
century.
If 14-digit numeric field:Time of the day and system date with out
century.
Date/Time operations
7/30/2019 Rpg Preset
55/144
Example:
DISPLAY SYSTEM DATE/TIME
0000.01 C Z-ADD0 NUM1 120
0001.00 C TIME NUM1
0002.00 C NUM1 DSPLY
0003.00 C SETON LR
7/30/2019 Rpg Preset
56/144
Call operations
BEGSRENDSR
BEGSRENDSR Is used to code a subroutine with in a program.BEGSR is
required at the beginning of every subroutine.the name of the subroutine is
mentioned in factor 1 of BEGSR.
EXSR
EXSR is used to call a subroutine and after the execution of the
subroutine,control return back to the next statement after EXSR.the name of
the subroutine is given in factor 2 position.
7/30/2019 Rpg Preset
57/144
O/p
DSPLY BEFORE ,DSPLY INSIDE,DSPLY AFTER
NOTE: If the name of the subroutine is changed to *INZSR then, the sequence of themessages would be INSIDE,BEFORE,INSIDE,AFTER.
Example:
THE MESSAGES WILL BE DISPLAYED IN THE SEQUENCE
BEFORE,INSIDE,AFTER
0001.00 C 'BEFORE' DSPLY
0002.00 C EXSR DSPSR
0003.00 C 'AFTER' DSPLY
0004.00 C* RETURN0004.01 C SETON LR
0005.00 C DSPSR BEGSR
0006.00 C 'INSIDE' DSPLY
0007.00 C ENDSR
7/30/2019 Rpg Preset
58/144
CODE Factor 1 Factor 2 Result Field INDICATORS
CASxx Compared Compared Subroutine
name
HI LO EQ
CASXX.CASXX...END
This helps us in conditional execution of subroutine. Factor-1 and factor-2 are compared and if the condition satisfied the subroutine
named in the result field is executed.
Example:
HERE ADDSR,SUBSR,MULSRDIVSR WILL BE EXECUTED IF
OPT CONTAINS A.S,M,OR D RESPECTIVELY.0001.00 FDSPSELECT CF E WORKSTN
0002.00 C *IN03 DOWEQ *OFF
0003.00 C EXFMT OPCODE
7/30/2019 Rpg Preset
59/144
0005.00 C OPT CASEQ 'A' ADDSR
0006.00 C OPT CASEQ 'S' SUBSR
0007.00 C OPT CASEQ 'M' MULSR
0008.00 C OPT CASEQ 'D' DIVSR
0009.00 C ENDCS
0010.00 C ENDDO
0011.00 C SETON LR
0012.00 C* RETURN
0013.00 C ADDSR BEGSR
0014.00 C NUM1 ADD NUM2 RESULT
0015.00 C ENDSR
0017.00 C SUBSR BEGSR
0018.00 C NUM1 SUB NUM2 RESULT
0019.00 C ENDSR
7/30/2019 Rpg Preset
60/144
0020.00 C MULSR BEGSR
0021.00 C NUM1 MULT NUM2 RESULT
0022.00 C ENDSR0022.01 C DIVSR BEGSR
0022.02 C NUM1 DIV NUM2 RESULT
0022.03 C ENDSR
DSPSELECT_CASXX
0000.40 A R OPCODE
0000.50 A*%%TS SD 20050802 125252 QSECOFR REL-V4R4M0 5769-
PW1
0000.60 A CA03(03)
0000.70 A 12 34'B ='0000.80 A 10 34'A ='
0000.90 A 14 34'OPT ='
0001.00 A 16 34'C ='
7/30/2019 Rpg Preset
61/144
CALL :
CODE Factor 1 Factor 2 ResultField
INDICATORS
CALL Called
program name
HI LO EQ
CALL is used to execute another RPG program from inside an RPG program.theprogram in which the call statement is called as the parent or a calling program andthe program being called is termed as child or called program.
CALLED program has to be compiled before compiling a calling program.BECAUSEwhile compiling the calling program it looks for acompiled object of the calledprogram.and it is not found an error occurs .
0001.10 A NUM1 3S 0B 10 50
0001.20 A NUM2 3S 0B 12 50
0001.30 A RESULT 5S 0O 16 50
0001.40 A OPT 1A I 14 50
7/30/2019 Rpg Preset
62/144
CALLSUB(Called program)0000.01 C 1 DO 100 NUM
0000.02 C ADD NUM SUM
0000.04 C ENDDO
0000.05 C SUM DSPLY
0000.06 C RETRN
0000.07 C *INZSR BEGSR
0001.01 C Z-ADD0 SUM 80
0002.00 C Z-ADD0 NUM 30
0003.00 C ENDSR
O/p DSPLY BEFORE, DSPLY 5050,DSPLY AFTER
Example:
CALLING PROGRAM(THIS PROGRAM CALLSUB PROGRAM WHICH FINDS
THE SUM OF FIRST 100 NATURAL NUMBERS.
0002.00 C 'BEFORE' DSPLY
0003.00 C CALL 'CALLSUB'
0004.00 C 'AFTER' DSPLY
0005.00 C RETRN
7/30/2019 Rpg Preset
63/144
PLIST & PARM
When parameters are passed into called program,it should be capable enough toreceive the same.for that reason we code PLIST statement at the top of the calledprogram.
*ENTRY Should appear in factor1 of PLIST command followed by PARMs.
Example:*ENTRY PLIST
PARM A
PARM BPARM C
POINTS TO NOTE:
1.The number of parameters passed and receiving should be the same.
2.Their attributes(data type and size) should preferably the same.
3.the order of parameters should be the same.
4.can have different names or same names in called and calling programs.
7/30/2019 Rpg Preset
64/144
Example: PLISTPP_NM
PROGRAM FOR CALCULATION OF SIMPLE INTEREST
0001.00 FDSPARR CF E WORKSTN
0001.01 C LIST1 PLIST
0002.01 C PARM P 5 0
0002.02 C PARM R 4 0
0002.03 C PARM T 2 0
0002.04 C PARM SI 7 0
0003.00 C PARM A 8 0
0004.00 C EXFMT INTEREST
0004.01 C CALL 'PLISTSUB' LIST10004.02 C EXFMT INTEREST
0005.00 C SETON LR
7/30/2019 Rpg Preset
65/144
PLISTSUB_NM(SUBPROGRAM)
0001.00 C *ENTRY PLIST
0002.00 C PARM P1 5 0
0003.01 C PARM R1 4 0
0003.02 C PARM T1 2 0
0003.03 C PARM SI1 7 0
0004.00 C PARM A1 8 0
0004.01 C P1 MULT R1 SI1
0005.00 C T1 MULT SI1 SI1
0006.00 C DIV 100 SI10007.00 C SI1 ADD P1 A1
0008.00 C RETURN
7/30/2019 Rpg Preset
66/144
DSPARR_DSPLAYFILE
0004.20 A R INTEREST
0004.40 A 5 27'SIMPLE INTEREST CALCULATION
0004.70 A 6 27'==========================
0004.90 A 9 10'ENTER PRINICIPLE :'
0005.10 A 11 10'ENTER RATE :'
0005.30 A 13 10'ENTER TIME :'0005.50 A 15 10'SIMPLE INTEREST IS :'
0005.70 A 17 10'AMOUNT IS :'
0005.90 A 20 41'PRESS ENTER TO CONTINUE'
0006.30 A P 5S 0B 9 33
0006.40 A R 4S 0B 11 33
0006.50 A T 2S 0B 13 33
0006.60 A SI 7S 0O 15 33
0006.70 A A 8S 0O 17 33
7/30/2019 Rpg Preset
67/144
File operations
READ :The READ operation reads the record from a file.WRITE : The WRITE operation writes a new record into the file.
READE :The READE operation retrieves the next sequence recordfrom a file.
READP :READP (read prior) operation reads the prior record from a file.
READPD :The READPF operation is just a combination of READE and READP.
READC :The READC operation can be used only with an externally describedworkstation file to obtain the next changed record in a sub file.
SETGT & SETLL : SETGT & SETLL only positions the record pointer and doesnot read the records.
UPDAT : The UPDAT operation is modify the records of an update disk file.(U in file type I.e position 15 of F-Spec).
7/30/2019 Rpg Preset
68/144
PROGRAM FOR FILE READ
FILEREAD_RPG0001.00 FEMPMASTER IF E K DISK
0002.00 FDSPLYFILE CF E WORKSTN
0002.01 C READ EMPRECFMT 10
0005.00 C DOW *IN10=*OFF
0006.00 C EXFMT READING
0007.00 C READ EMPRECFMT 100008.00 C ENDDO
0009.00 C SETON LR
EMPMASTER_PF
0001.00 A R EMPRECFMT0002.00 A EMPNUMBER 5
0003.00 A EMPNAME 10
0004.00 A EMPSALARY 10 2
0005.00 A K EMPNUMBER
7/30/2019 Rpg Preset
69/144
DSPLYFILE_DISPLAY FILE0001.60 A R READING
0001.90 A CA03(03)0002.00 A 5 33'KARISHMA S/W LTD'
0002.10 A DSPATR(UL)
0002.20 A DSPATR(HI)
0002.30 A 8 38'EMPNO:'
0002.40 A 10 37'EMPNAME:'
0002.50 A 12 32' '
0002.60 A 12 29' '
0002.70 A 12 27' '
0002.80 A EMPNUMBER R B 8 46REFFLD(EMPRECFMT/EMPNUMBER
0002.90 A MINDADB/EMPMASTER)
0003.00 A EMPNAME R B 10 46REFFLD(EMPRECFMT/EMPNAME0003.10 A MINDADB/EMPMASTER)
7/30/2019 Rpg Preset
70/144
7/30/2019 Rpg Preset
71/144
IntroductionData structures allow to define an area of space within memory to store
individual elements of data.
Purpose: Subdividing fields into subfields
Restructure records into different layouts
Change field data types
Define character fields longer than 256 bytes To add second dimension to arrays
Data structures can also be defined externally.
TYPES: Simple Data Structure
External data structure
Data Area Data structure
File information data structure
Program status data structure
7/30/2019 Rpg Preset
72/144
Simple Data Structure To define data structure specify DS in 24-25.
Follow the definition with sub field definitions.
From to positions indicate the sub fields positions on IPD prompt.
Examples:DISPLAY SYSTEM DATE AND TIME & YEAR
0001.00 DDATE1 DS
0002.00 DDD 1 2
0003.00 DMM 3 4
0004.00 DYYYY 5 8
0005.00 C MOVEL UDATE DATE1
0005.01 C DD DSPLY0005.02 C MM DSPLY
0005.03 C YYYY DSPLY
0006.00 C DATE1 DSPLY
0007.00 C SETON LR
7/30/2019 Rpg Preset
73/144
READ DATA FROM A DATA BASE FILE USING EXTERNAL DS.
External Datastructures
FFLTFILE IF F 112 DISK
FEMPPF UF A E DISK
DEMPFILE E DS EXTNAME(EMPPF)
DID 1 6
DNAME 7 17
DSTATUS 18 19
C *LOVAL SETLL FLTFILE
C READ FLTFILE 90
C DOW *IN90 = *OFF
C EVAL EMPFILE = FLTFILE
C
C WRITE EREC 90
C ENDDO
C SETON LR
External data structure
7/30/2019 Rpg Preset
74/144
File information data structure
FMaintCust CF E Workstn INFDS(DSDisplay)
DSDisplay DS
D FileName *File
DFileOpen 9 9
DFileeof 10 10
DMsgId 46 52
DSCRSize *SIZE
DKeyPress 369 369
DCursorloc 370 371B 0
INFDS keyword is specified in the F-spec against the file declaration.
7/30/2019 Rpg Preset
75/144
File Information Data Structure:*FILE File Name
*RECORDExternal file RECORD FORMAT
Program file RecordID
*OPCODE
*STATUS Error Code
*ROUTINE Name of the routine where error occurred.
File Status codes00000 - No Error
00013 - Sub file Full
01021 - Duplicate Key Not allowed
01041 - Array/Table Sequence error
01211 - I/O operation to closed file
01215 - OPEN issued to file already open01218 - Unable to allocate Record
01221 - Update without prior read
01241 - Record number not found
01331 - Wait time exceeded for read from work station file.
7/30/2019 Rpg Preset
76/144
Program Status Data structureDS.PSSR_DS
0002.00 IDS1 SDS
0003.01 I *STATUS ERR
0003.04 C Z-ADD100 NUM1 30
0029.00 C Z-ADD0 NUM2 30
0029.01 C NUM1 DIV NUM2 NUM3 300029.02 C RETRN
0029.03 C *PSSR BEGSR
0033.00 C ERR IFEQ 102
0033.01 C 'ZERO DIV'DSPLY
0033.02 C ENDIF
0033.03 C* MOVE 'CANCLE' RET 6
0045.04 C ENDSR
7/30/2019 Rpg Preset
77/144
00121- Invalid array index
00202 Called program failed
00211- Called program not found
00231 Called program Halt indicator On
00333- Error on DSPLY operation
00414- Not authorized to use data area
00415 Not authorized to update data area
00431- Data area previously locked00907- Decimal data error
09999 System program exception error
Program Status codes
O/P
DSPLY ZERO DIV
DS.PSSR 2901 tried to divide by zero (factor 2)
7/30/2019 Rpg Preset
78/144
Program for Data AreasAUTOMATIC GENARATION OF VEHICAL REGISTRATION NUMBER
0001.00 FDSPARR CF E WORKSTN
0001.02 E LETTER 1 26 1
0002.00 IDS1 UDS
0002.01 I 1 2 STATE
0003.01 I 3 40AREA
0003.02 I 5 5 ALPHA
0003.03 I 6 90SRLNO
0007.00 C *NAMVAR DEFN AREA0 9
0008.00 C *IN03 DOWEQ*OFF
0009.00 C *LOCK IN AREA0
0010.00 C MOVE AREA0 DS1
7/30/2019 Rpg Preset
79/144
0011.00 C SRLNO IFEQ 9999
0012.00 C ALPHA IFEQ 'Z'
0013.00 C EXSR ADARA
0014.00 C ELSE
0015.00 C EXSR ADALPA
0016.00 C ENDIF
0017.00 C ELSE0018.00 C EXSR ADSLNO
0019.00 C ENDIF
0020.00 C MOVE DS1 FLD001
0021.00 C EXFMTVEHREG
0022.00 C EXSR UPDSR
0023.00 C ENDDO
0024.00 C SETON LR
7/30/2019 Rpg Preset
80/144
0025.00 C* ERRRTN BEGSR
0025.01 C* RETRN
0026.00 C ADARA BEGSR
0027.00 C ADD 1 AREA
0028.00 C MOVE 'A' ALPHA
0029.00 C Z-ADD1 SRLNO
0030.00 C ENDSR0031.00 C ADALPA BEGSR
0032.00 C 1 DO 26 I 20
0033.00 C ALPHA IFEQ LETTER,I
0034.00 C LEAVE
0035.00 C ENDIF
0036.00 C ENDDO
0037.00 C ADD 1 I
0038.00 C MOVE LETTER,I ALPHA
7/30/2019 Rpg Preset
81/144
0040.00 C Z-ADD1 SRLNO
0041.00 C ENDSR
0043.00 C ADSLNO BEGSR
0044.00 C ADD 1 SRLNO
0045.00 C ENDSR
0045.01 C UPDSR BEGSR
0045.02 C MOVE DS1 AREA0
0045.03 C *LOCK OUT AREA0
0045.04 C ENDSR V
0046.00 **
0047.00 A
0048.00 B ------------z
7/30/2019 Rpg Preset
82/144
DSPARR_DISPALYFILE0002.40 A R VEHREG
0002.50 A*%%TS SD 20050801 182655 QSECOFR REL-V4R4M0 5769-PW1
0002.60 A CA03(03)
0002.70 A 8 15'AUTOMATIC GENERATION OF VE
0002.80 A GISTRATION NUMBER'
0003.50 A 13 20'NEW VEHICAL REGISTRATON NUM:
0003.90 A FLD001 12A O 13 60
0004.00 A 18 56'F3 = EXIT'
DisplayData Area
Data area . . . . . . . : AREA0
Library . . . . . . . : MINDADB
Type . . . . . . . . . : *CHAR
Length . . . . . . . . : 9
Value 'AP01A0001'
7/30/2019 Rpg Preset
83/144
16-20 Previous status code
21-28 RPG Source sequence number40-46 Exception Error ID MCH or CPF
201-208 Name of the file last used
244-253 Job Name
254-263 User name
264-269 Job Number
276-281 --System Date in Udate Format
288-293 Date Compiled
304-333 Source file/library/member
Structure of PSDS
7/30/2019 Rpg Preset
84/144
7/30/2019 Rpg Preset
85/144
1. A subfile is a series of similar records that can be displayed simultaneously on the
screen.
Subfiles
2. When you load data to a subfile data remains in main memory until subfile iswritten on to the screen.
3.When subfile is written on to the screen data is passed on to the workstationcontroller.
4. Controller assumes responsibility for handling subfile data on screen until control isreturned to the program.
5. A subfile can hold a maximum of 9999 records.
6. At a time 24 subfiles can be active.
7/30/2019 Rpg Preset
86/144
Components of a subfile
Two record formats are requiredSFL - Subfile record format and
SFLCTL - Subfile Control Record format.
The Subfile Record format holds the actual data and is similar to the database file.
The Subfile Control Record format Describes the headings for the fields and
controls the subfile size, Display, Number of records and specifies how data is
retrieved and what is to be done on data in the subfile on various events.
7/30/2019 Rpg Preset
87/144
SUBFILE TYPES
Load All Subfile
Expanded Load
Single Page Load
7/30/2019 Rpg Preset
88/144
All records are loaded at once.
Maximum 9999 records can be loaded.
Applicable for files with fewer number of records.
ROLLUP and ROLLDOWN operations are automatically handled by theprogram.
Control returns to the program once the user moves past the last record in thesubfile.
7/30/2019 Rpg Preset
89/144
Expanded load
Loads enough records to fill first screen.
Rollup key impression brings control to the program.
First screen records stay in the subfile and new records are appended each time
rollup is pressed.
Roll down automatically handled by subfile.
7/30/2019 Rpg Preset
90/144
Page at a time subfile
Stores only page worth of records.
Program handles both rollup and roll down keys.
Subfile is cleared and new page of records are loaded on each key impression afterclearing the subfile.
Rollup/page down load next set of records.
Roll down/page up previous set of records are loaded .
Subfile preferred for database having large set of records works without impactingperformance.
7/30/2019 Rpg Preset
91/144
Key WordsSFLCTL
Defines Subfile control record format
Links subfile record format with Subfile format
One subfile format maps to one and only one subfile control format and
vice versa
SFLSIZ
Specifies size of the subfile. No of records that subfile can hold.Can be
hard coded or passed as a hidden field ,S type, 0 Decimals and 5 digits long.
SFLPAG
Specifies number of records that appear on a single screen and must be less
than or equal to SFLSIZ
7/30/2019 Rpg Preset
92/144
SFLDSPCTL and SFLCTL
Normally conditioned with indicators to display control format or the subfile. Empty subfile- (SFLCTL) cannot be displayed so indicator is seton when subfile i
or required to be displayed.
Having indicator for SFLDSPCTL is optional.
SFLCLR
Normally SFLCLR will have the negative conditioning indicator that is given for S
and SFLDSPCTL.
When indicator is set on it will clear the records in the subfile.
7/30/2019 Rpg Preset
93/144
Program for Load all sub file0001.00 FEXPPF IF E DISK
0002.00 FLOADDSP CF E WORKSTN
0003.00 F SFILE(DATA1 : RRN)
0004.00 DRRN S 4 0
0005.03 ** CLEAR SFL
0005.05 C SETON 1020400007.00 C WRITE HEADING1
0007.01 C SETOFF 40
0008.01 C Z-ADD 1 RRN 4 0
0008.02 ** LOADING DATA
0008.03 C READ PFREC03 90
0009.00 C DOW *IN90=*OFF
0009.01 C WRITE DATA1
7/30/2019 Rpg Preset
94/144
0011.00 C ADD 1 RRN
0013.00 C READ PFREC03 90
0013.01 C ENDDO
0014.01 ** DSPLY SFL
0014.02 C DOW *IN03=*OFF
0014.03 C SETON 10200014.04 C WRITE FOOTING
0016.00 C EXFMT HEADING1
0017.00 C SETOFF 1020
0017.01 C ENDDO
0017.02 **
0018.00 C SETON LR
7/30/2019 Rpg Preset
95/144
EXPPF_LOADALL
0000.01 UNIQUE
0001.00 A R PFREC03
0002.00 A PFEMNO 7 COLHDG('EMPLOYEE NUMBER')
0003.00 A PFEMNA 25 COLHDG('EMPLOYEE NAME')
0004.00 A PFADD1 10 COLHDG('ADDRESS1')
0005.00 A PFADD2 10 COLHDG('ADDRESS2')
0006.00 A K PFEMNO
LOADDSP_DISPALYFILE
0000.40 A R DATA1 SFL
0000.60 A PFEMNO R O 13 17REFFLD(PFREC03/PFEMNO MINDADB
0000.70 A PFEMNA R O 13 27REFFLD(PFREC03/PFEMNA MINDADB
0000.80 A PFADD1 R O 13 53REFFLD(PFREC03/PFADD1 MINDADB
7/30/2019 Rpg Preset
96/144
0000.90 A R HEADING1 SFLCTL(DATA1)
0001.10 A CA03(03)
0001.20 A 10 SFLDSP
0001.30 A 20 SFLDSPCTL
0001.40 A 30 SFLINZ
0001.50 A 40 SFLCLR0001.60 A 50 SFLEND(*MORE)
0001.70 A SFLSIZ(0050)
0001.80 A SFLPAG(0005)
0001.90 A 6 36'EMPDETAILS'
0003.10 A 11 19'NUMBER'
0003.30 A 11 37'NAME'
0003.50 A 11 54'ADDRESS1'
7/30/2019 Rpg Preset
97/144
Program for Expanded Load0001.00 FEXPPF IF E DISK
0002.00 FEXPDSP CF E WORKSTN
0003.00 F RRN KSFILE SFLREC
0004.00 C *IN03 DOWEQ*OFF
0004.02 C*SFL CLEAR
0005.00 C SETON 400006.00 C WRITESFLCTL
0007.00 C SETOF 40
0007.01 C*
0007.02 C Z-ADD1 RRN 40
0007.04 C STR TAG
0007.05 C SETOF 20
0007.06 C Z-ADD1 RRNCT 40
0009.04 C**
7/30/2019 Rpg Preset
98/144
0009.05 C RRNCT DO 10
0012.00 C READ EXPPF 33
0013.00 C *IN33 IFEQ *ON
0014.00 C MOVE *ON *IN50 END
0015.00 C ELSE
0016.00 C RRN IFLT 99990017.00 C WRITESFLREC
0017.01 C ADD 1 RRN
0017.02 C ADD 1 RRNCT
0017.03 C ENDIF
0018.00 C ENDIF
0020.00 C ENDDO
0022.03 C SETON 20
0023.00 C EXFMTSFLCTL
7/30/2019 Rpg Preset
99/144
0023.01 C* PAGE DOWN
0024.00 C *IN35 IFEQ *ON
0025.00 C GOTO STR
0026.00 C ENDIF
0026.01 C ENDDO
0028.00 C SETON LR
EXPDSP_DISPLAYFILE
0000.40 A CF03(03)
0000.50 A R SFLREC SFL
0000.70 A PFEMNO R O 6 4REFFLD(PFREC03/PFEMNO MINDA
0000.90 A PFEMNA R O 6 14REFFLD(PFREC03/PFEMNA MINDA
0001.10 A PFADD1 R O 6 40REFFLD(PFREC03/PFADD1 MINDA
0001.30 A PFADD2 R O 6 62REFFLD(PFREC03/PFADD2 MINDA
7/30/2019 Rpg Preset
100/144
0001.50 A R SFLCTL SFLCTL(SFLREC)
0001.70 A ROLLUP(35)
0001.80 A 20 SFLDSP
0001.90 A 20 SFLDSPCTL
0002.00 A 40 SFLCLR
0002.10 A 50 SFLEND(*MORE)0002.20 A SFLSIZ(0011)
0002.30 A SFLPAG(0010)
0002.40 A 1 29'EXPANDING SUBFILE '
0001.90 A 6 36'EMPDETAILS'
0003.10 A 11 19'NUMBER'
0003.30 A 11 37'NAME'
0003.50 A 11 54'ADDRESS1'
7/30/2019 Rpg Preset
101/144
Program for Single page load
0001.00 FPAGEPF IF E DISK
0002.00 FPAGEDSP CF E WORKSTN
0003.00 F RRN KSFILE SFLREC
0003.02 I* 10 C CRSIZE
0003.03 C Z-ADD10 CRSIZE 40
0003.04 C EXSR CLE
0003.05 C EXSR LOA
0003.06 C *IN03 DOWEQ*OFF
0003.07 C SETON 10
0003.08 C WRITEFOOT
0003.09 C EXFMTSFLCTL
7/30/2019 Rpg Preset
102/144
0007.01 C* PAGE DOWN KEY PRESSED
0007.02 C *IN22 IFEQ *ON
0007.03 C EXSR PGD
0007.04 C ENDIF
0007.05 C* PAGE UP KEY PRESSED
0007.07 C *IN23 IFEQ *ON0007.08 C EXSR PUP
0007.09 C ENDIF
0007.10 C ENDDO
0007.11 C SETOF 10
0007.12 C SETON LR
7/30/2019 Rpg Preset
103/144
0007.13 C* =====================PAGE DOWN==================
0007.14 C PGD BEGSR
0007.15 C LKEY SETGTPAGEPF
0007.16 C READ PAGEPF 31
0007.17 C *IN31 IFEQ *OFF
0007.18 C LKEY SETGTPAGEPF
0007.19 C EXSR CLE
0007.20 C EXSR LOA
0007.21 C ENDIF
0007.22 C ENDSR
0007.23 C* =====================PAGE UP=======================
0007.24 C PUP BEGSR
0007.25 C FKEY SETLLPAGEPF
0007.26 C READ PAGEPF 31
7/30/2019 Rpg Preset
104/144
0007.27 C *IN31 IFEQ *OFF
0007.28 C FKEY SETLLPAGEPF
0007.29 C Z-ADDCRSIZE SRCNT 20
0007.31 C DO CRSIZE
0007.32 C READPPAGEPF 31
0007.33 C MOVE PFEMNO FKEY
0007.34 C *IN31 IFEQ *ON
0007.35 C ENDIF
0007.36 C ENDDO
0007.37 C EXSR CLE
0007.38 C FKEY SETLLPAGEPF
0007.40 C EXSR LOA
0007.41 C ENDIF
0007.42 C ENDSR
7/30/2019 Rpg Preset
105/144
0008.00 C* ===================LOADING DATA===========
0009.00 C LOA BEGSR
0009.01 C Z-ADD1 RRN 40
0009.02 C SETOF 10
0009.03 C READ PAGEPF 31
0009.05 C *IN31 IFEQ *OFF
0009.06 C MOVE PFEMNO FKEY 70
0009.07 C ENDIF
0009.10 C RRN DOWLECRSIZE
0009.11 C *IN31 ANDNE*ON
0009.13 C WRITESFLREC
0009.14 C MOVE PFEMNO LKEY 70
0009.15 C ADD 1 RRN
0009.16 C READ PAGEPF 31
0009.17 C ENDDO
7/30/2019 Rpg Preset
106/144
0009.19 C *IN31 IFEQ *ON
0009.20 C SETON 50
0009.21 C ELSE
0009.22 C SETOF 50
0009.23 C ENDIF
0009.24 C ENDSR
0009.25 C*================== CLEAR SCREEN =======================
0009.26 C CLE BEGSR
0010.00 C SETON 40
0011.00 C WRITESFLCTL
0012.00 C SETOF 40
0013.00 C ENDSR
7/30/2019 Rpg Preset
107/144
PAGEPF_PHYSICALFILE:
SAME AS EXPANDED LOAD
PPAGEDSP_DISPLAYFILE
0000.40 A CF03(03)
0000.50 A R SFLREC SFL
0000.70 A PFEMNO R O 7 3REFFLD(PFREC03/PFEMNO MINDA
0000.90 A PFEMNA R O 7 18REFFLD(PFREC03/PFEMNA MINDA0001.10 A PFADD1 R O 7 44REFFLD(PFREC03/PFADD1 MINDA
0001.30 A PFADD2 R O 7 57REFFLD(PFREC03/PFADD2 MINDA
0001.50 A R SFLCTL SFLCTL(SFLREC)
7/30/2019 Rpg Preset
108/144
0001.70 A PAGEDOWN(22)
0001.80 A PAGEUP(23)
0001.90 A 10 SFLDSP
0002.00 A 10 SFLDSPCTL
0002.10 A 40 SFLCLR
0002.20 A 50 SFLEND(*MORE)
0002.30 A SFLSIZ(0011)
0002.40 A SFLPAG(0010)
7/30/2019 Rpg Preset
109/144
7/30/2019 Rpg Preset
110/144
Introduction
Arrays and tables allow program to organize groups of related data elements for easy
retrieval.
Array consists of series of elements with like characteristics (data type and size).
Here every element in an array is accessed by means of an index which indicates the
relative position of the element from the beginning of the array.The numbering startsfrom 1.
D l ti f A
7/30/2019 Rpg Preset
111/144
Declaration of Arrays
Arrays are declared on the D-spec, by specifying the array name,entries per
record,entries per table and length of an entry.
DABC 10A Dim(10) PERRCD(1) CTDATA
Here
Array name = ABC
Entries per record =1Entries per table = 10
Length of entry =10
This is an array of 10 elements,each element of size 10 bytes.
Types of Arrays
7/30/2019 Rpg Preset
112/144
Types of Arrays
Compile time Array
Preruntime Array
Runtime Array
7/30/2019 Rpg Preset
113/144
Compile time Array
Compile time array elements are facilitated at the end of the code.
Preruntime array elements are facilitated before the execution of the program.
Run time array elements are facilitated from the data base,they are loaded into array
during runtime.
Preruntime Array
Runtime Array
P f il i
7/30/2019 Rpg Preset
114/144
Program for compile time array0000.01 ** COMPILE TIME ARRAY USING MULTIDIMENSION ARRAY
0001.00 DABC S 15A Dim(12) PERRCD(1) CTDATA
0002.00 DI S 4S 0 INZ(1)
0003.00 C I DOWLE12
0004.00 C ABC,I DSPLY
0006.00 C XYZ,I DSPLY
0007.00 C ADD 1 I
0008.00 C ENDDO
0009.00 C SETON LR
0010.00 **
0011.00 JANUARY 31
0012.00 FEBRUARY 28
7/30/2019 Rpg Preset
115/144
0013.00 MARCH 31
0014.00 APRIL 30
0015.00 MAY 31
0016.00 JUNE 30
0017.00 JULY 30
0018.00 AUGUST 31
0019.00 SEPTEMBER 30
0020.00 OCTOBER 31
0021.00 NOVEMBER 30
0022.00 DECEMBER 31
7/30/2019 Rpg Preset
116/144
Program for preruntime time array0001.00 FPF1 IT F 30 DISK
0002.00 DARR1 S 30 DIM(10) FROMFILE(PF1)
0003.00 DI S 4 0 INZ(1)
0004.00 C DO 10
0005.00 C ARR1(I) DSPLY
0006.00 C EVAL I = I + 10007.00 C ENDDO
0008.00 C SETON LR
St i ti
7/30/2019 Rpg Preset
117/144
String operationsCAT
The CAT operation concatenates the character strings specified in factor2 to the
end of the character specified in factor1 and places it in the result field.
SCAN
The SCAN operation scans a character string(base string)contained in factor 2 for
a sub string(compare string)contained in factor1.if the string is found that,then the
position where it is found is placed in the result field.
XLATE
XLATE(translate)is used to find for a sub string in a string and placed it with
another one and puts the resulting string into the result field.
Factor 1 must contain the find string,followed by a colon,followed by the replace
string.
7/30/2019 Rpg Preset
118/144
SUBST
The SUBST operation returns a sub string from factor2,starting at the locationspecified in factor 2 for the length specified in factor 1.and places this sub string in
the result field.if factor 1 is not specified,the length of the string from the first
position is used.
CHECK
The CHECK operation checks a mismatched character string(base
string)contained in factor 2 for a sub string(compare string)contained in factor1.if
the string is found that,then the position where it is found is placed in the result
field.
Program for string opcodes
7/30/2019 Rpg Preset
119/144
0000.01 **STRING CONCAT
0000.02 C MOVE 'ANIL' S1 4
0000.03 C MOVE 'KUMAR' T1 5
0001.00 C S1 CAT T1:1 P1 10
0002.00 C P1 DSPLY
0002.03 **SCAN METHOD
0002.04 C 'ABC' SCAN 'DEFABC' NUM 100 90
0002.05 C NUM DSPLY
0002.06 **CHECK
0002.07 C 'ABC' CHECK'DEFABC' NUM 100 90
0002.08 C NUM DSPLY
Program for string opcodes
7/30/2019 Rpg Preset
120/144
0002.09 **SUBSTRING
0002.10 C Z-ADD4 S 20
0002.11 C MOVEL'ABCDEF' S2 10
0002.12 C 2 SUBSTS2:S P2 10
0002.13 C P2 DSPLY
0002.14 **XLATE STRING
0002.15 C MOVEL'999B9999'NUM1 8
0002.16 C 'B':'-' XLATENUM1 FLD1 8
0002.17 C FLD1 DSPLY
0003.00 C SETON LR
7/30/2019 Rpg Preset
121/144
O/p
DSPLY ANIL KUMAR
DSPLY 4
DSPLY 1
DSPLY DE
DSPLY 999-9999
C-spec---prompt
pmt SeqNbr Lvl N01 N02N03 Factor 1 OpCode Factor 2 Result
C 0002.04 'ABC' SCAN 'DEFABC' NUM
Len Dec H/N/P Hi Lo Eq Comment
10 0 90
7/30/2019 Rpg Preset
122/144
OUTPUT SPECIFICATION
RPG/400
Session 5
O-Spec
7/30/2019 Rpg Preset
123/144
Describe the record and the format of fields in a program-described output file and
when the record is to be written.
Optional for an externally described file.
Divided into two categories:
Record identification and control (positions 7 through 51),
Field description and control (positions 21 through 80).
O-Spec
O t t ifi ti l t E t ll D ib d fil
7/30/2019 Rpg Preset
124/144
Output specification layout Externally Described files1. Record Identifier specifications
Columns Descriptions
1-5 Sequence number
6 O
7-15 Record Name
16-18 Logical Relationship AND,OR
17 Type of record to write:
D Detail
E Exception (EXCEPT)
H Detail (header)
T Total
Record Identifier specifications Continued 2
7/30/2019 Rpg Preset
125/144
Record Identifier specifications Continued .. 2
Columns Descriptions
18 Release Record after output : R or Blank
18-20 Record Addition : ADD, DEL
21-29 Output Conditioning indicators:Blank,01-99,KA-KN,KP-KY,L1-L9,H1-H9,U1-
U8,MR,LR,RT,1P
30-39 EXCEPT name
40-80 (Blank)
81-100 Comments
2 Field Description Specifications
7/30/2019 Rpg Preset
126/144
2. Field Description Specifications
Columns Descriptions
1-5 Sequence number
6 O
7-20 (BLANK)
21-29 Output Conditioning indicators: Blank,10-99,KA-KN,KP-KY,L1-L9,H1-H9,U1-
U8,MR,LR,RT,1P
30-43 Field Name or *ALL
44 (BLANK)
45 Result field contents after writing record : Blank or B
46-80 (BLANK)
81-100 Comments
Program for O-spec
7/30/2019 Rpg Preset
127/144
Program for O-spec0001.00 FPFNAME IP E K DISK
0002.00 FQSYSPRT O F 80 OF PRINTER
0003.00 OQSYSPRT H 2 2 2 1P
0003.02 O UDATE Y 70
0004.00 O 40 'ABC COMPANY'
0005.00 O*
0005.02 OQSYSPRT H 2 1P
0005.03 O 40 '==========='
0006.00 OQSYSPRT H 2 1P
0007.00 O 20 'EMPLOYEE DETAILS'
0007.01 OQSYSPRT H 2 1P
0007.02 O 20 '================'
0007.03 O* '
7/30/2019 Rpg Preset
128/144
0009.00 O H 2 1P
0010.00 O 35 'EMP NUMBER'
0011.01 O 53 'EMPLOYEE NAME'
0012.00 O 73 'EMPLOYEE SALARY'
0012.01 OQSYSPRT H 2 1P
0012.02 O 47 '========================'
0012.03 O 71 '========================
0012.04 O 74 '==='
0012.05 O*
7/30/2019 Rpg Preset
129/144
0012.06 OQSYSPRT D 2 N1P
0013.00 O NUMBER 36
0013.01 O* SALARY 68
0013.02 O* NAMERY 69 ' . '
0014.00 O NAME 68
0015.00 O SALARY 69 ' . '
0016.00 O 71 'RS'
PFNAME_OSPEC
0001.00 A R RECNAME
0002.00 A NUMBER 10
0003.00 A NAME 30
0004.00 A SALARY 10 2
0005.00 A K NUMBER
7/30/2019 Rpg Preset
130/144
CONTROL SPECIFICATION
RPG/400
Session 6
HEADER SPECIFICATION
7/30/2019 Rpg Preset
131/144
HEADER SPECIFICATIONColumns Description
1-5 Sequence number
6 H
7-80 Control Key Words
81-100 Comments
Key words in HSPEC
7/30/2019 Rpg Preset
132/144
Key words in HSPEC
Key Word Description
COPYRIGHT(string) Provides copyright information to include in a module
CURSYM(sym) Character to use a s currency symol enclosed in quotes
DATFMT(fmt{seperator}) Format of date literals defaults to *ISO
DEBUG(*YES|*NO) Enables Dump opcode
NOMAIN Indicates that there is no main procedure in this module
TIMFMT(fmt{seperator}) Format of Time literals defaults to *ISO
DFTNAME(rpg_name) Default name of RPG program or module
Standard control specification
7/30/2019 Rpg Preset
133/144
pThree methods to provide control information to the compiler.
H-specs in the source
*LIBL/RPGLEHSPEC(user defined data area)
QRPGLE/DFTLEHSPEC(IBM-supplied data area)
7/30/2019 Rpg Preset
134/144
CRTDTAARA DTAARA(QGPL/RPGLEHSPEC)+
TYPE(*CHAR) +LEN(26) +
VALUE(DATFMT(*YMD/) TIMFMT(*HMS)) +
TEXT(Default RPG H-SPEC)
OR
CRTDTAARA DTAARA(QPRGLE/DFTLEHSPEC)+
TYPE(*CHAR) +
LEN(26) +
VALUE(DATFMT(*YMD/) TIMFMT(*HMS)) +
TEXT(Default RPG H-SPEC)
OR
In the program typeH DATFMT(*YMD/)
H TIMFMT(*HMS)
RPG LIMITS
7/30/2019 Rpg Preset
135/144
File Name 10
No of files in a program *
Record format name 10
Field name 10
Character field size 32767
No of decimal places 30
Data structure name 10
Data structure size 32767
Array name 10
Number of array elements 32767
Named constant size 1024
No of subroutines in a program *
Size of program *
7/30/2019 Rpg Preset
136/144
DEFINITION SPECIFICATION
RPG/400
Session 7
The Definitive Specification
7/30/2019 Rpg Preset
137/144
The DefinitiveSpecification
Definition specification caters to the data definition requirements.
Arrays
Data Structures
Work fields
Data Areas
Named Constants
Procedure Prototypes are defined here.
D-Spec Layout
7/30/2019 Rpg Preset
138/144
p y
Columns Description
1-5 Sequence Number
6 D
7-21 Definition Name(Data structure,sub field,standalone
field,named constant)
22 Externally Described:
(Blank) Program Described
E Externally described
23 Data Structure Type:(Blank) Miscellaneous definition
S Program status data structure
U Data area data structure
Key Words
7/30/2019 Rpg Preset
139/144
y
Key Word Description
ALIGN Aligns data structure sub fields of integer data type on two or fourbyte boundaries.
ALT(array_name) Names a main array for use with this alternating array
ASCEND Array or table entries are in ascending order.
BASED(Basing_pointer_name) Names a pointer to the address of the data item
CONST(Constant) Specifies a value of the named constant or indicates that a passedparameter is read only.
CTDATA Specifies a compile time table or array
DATFMT Default date format and separator
DESCEND Array or table entries are in descending order
DIM (numericconstant)
Defines the number of elements in an array or a table
Key Word Description
7/30/2019 Rpg Preset
140/144
DTAARA(dataareaname) Associates the data item with the external data area:
Data_area_name,*LDA,*PDA
EXPORT Allows the data item to be accessed by another ILE module,using the IMPORT Key word.This ILE module allocates thestorage.
EXTFLD(field_name) Renames a subfield in an externally described datastructure.
EXTFMT(Code) External Data type for Compile time and pre runtime tables.B Binary I- signed integer L-left sign
P Packed decimal R- Right sign S Zoned decimal
EXTNAME(Filename:{formatname})
File Containing field descriptions used for subfields in adata structure.
EXTPGM(program name) Names a program whose prototype is being defined.
EXTPROC(procedure name) Names a procedure whose prototype is being defined.
FROMFILE(file_name) File with input data for pre runtime array or table.
Key Word Description
7/30/2019 Rpg Preset
141/144
IMPORT Storage for data item is allocated in another ILE module with theEXPORT Keyword.This module can use the same storage.
INZ(constant) Initializes the data item
LIKE(RPGname) Data Items Attributes (length, type) are based upon anothervariable.
NOOPT Do not perform optimization for this data item
OCCURS(numericConstant)
Number of occurrences of a multiple occurrence data structure
OPDESC Passes operational descriptors with parameters defined within aprototype
OPTIONS Specifies one or more parameter passing options
*NOPASS Parameter need not be passed*OMIT *OMIT IS allowed for parameter
*VARSIZE Parameter may be shorter than the defined length
OVERLAY(name:{pos})
Data structure sub Field overlays the storage of another field
Key Word Description
7/30/2019 Rpg Preset
142/144
PACKEVEN Zeroes out the high order digit when pack field has even numberof digits
PERRCD(NumericConstant)
Number of elements per record of an array or table.
PREFIX(Prefixstring:{number of
Characters replaced})
String to be prefixed to sub field names of an externally describeddata structure
PROCPTR Defines an item as a procedure pointer
STATIC Defines a local variable with static storage, holding its valueacross procedure calls.
TIMFMT(format
{separator})
Default time format and the separator
TOFILE(Filename) File to which an array or table is to be written
VALUE Specifies that a parameter will be passed by value instead ofreference.
Program using D-spec
7/30/2019 Rpg Preset
143/144
g g p0002.00 Df8 S 8F INZ(-1)
0003.00 DI10 S 10I 0 INZ(-265)0005.01 C EVAL F8=%ABS(F8)
0005.02 C EVAL I10=%ABS(I10-365)
0006.01 C F8 DSPLY
0006.02 C I10 DSPLY
0008.00 C SETON LR
7/30/2019 Rpg Preset
144/144