1
Datastage – Basic Programming
2
Objective
• Constant , Variables• Functions• Subroutines • Operators• Reserved Words• System Variables• Compiler Directives• Job Control• Examples• Basic Rules
3
Constants
• A constant is a value that is fixed during the execution of a program, and may be reused in different contexts.
A constant can be:
• A character string
• An empty string• A numeric string in either floating-point or integer
format
4
Variables
• Variables are used for storing values in memory temporarily.
You can then use the values in a series of operations. The value of a variable can be:
• Unassigned• A string• An integer or floating-point number• The null value• A dimensioned array• A file variable
• DataStage provides a set of read-only system variables that store system data such as the current date, time, pathname, and so on..
5
Dimensioned Arrays
• One-dimensional arrays, or vectors• Two-dimensional arrays, or matrices
For example:
• A(1) ; *specifies the first element of variable A• Cost(n + 1) ; * specifies an expression to calculate the
index• Obj(3,1)• Widget(7,17)
6
Functions
• FunctionName (argument, argument)
• A function performs mathematical or string manipulations on the arguments supplied to it, and returns a value.
• Some functions have no arguments.
• Transform functions in DataStage must have at least one
argument that contains the input value to be transformed.• The transform function must return the transformed value
using a Return (value) statement.
7
Statements
Functionality• Changing program control.( for example, calling a
Subroutine, or defining a loop.)
• Assigning a value to a variable.• Specifying the value of a constant.
• Adding comments to programs.
8
Labels
• Statement Labels -
– unique identifier for a line of code.– Can be put either in front of a statement or on its own
line.
9
Subroutines
• A subroutine is a self-contained set of instructions that perform a specific task.
Two forms of subroutine :
– An embedded subroutine is contained within the program and is
accessed with a GoSub statement.– An external subroutine is stored in a separate file and is
accessed with a Call statement.
10
Subroutines
• DataStage is also supplied with a number of before/after subroutines, for
– running before or after a job or an active stage. – You can define your own before/after subroutines using
the DataStage Manager.
– Before/after subroutines must have two arguments. – The first contains the value a user supply to the – The second is the subroutine’s reply code. The reply
code is 0 if there was no error. Any other value indicates the job was stopped.
11
Special DataStage BASIC Subroutines
• Log events in the job’s log file using
– DSLogInfo, DSLogWarn, DSLogFatal, DSTransformError
• Execute DOS or DataStage Engine commands – DSExecute
All the subroutines are called using the Call statement.
12
Operators
• An operator performs an operation on one or more expressions.
• Types of Operators– Arithmetic operators– String operators for:– Concatenating strings with Cats or :– Extracting substrings with [ ]– Relational operators– Pattern matching operators– If operator– Logical operators– Assignment operators
13
Arithmetic Operators
• This table lists the arithmetic operators in order of evaluation:
• You can change the order of evaluation using parentheses.
Operator Operation Example– Negation –x^ Exponentiation x ^ y* Multiplication x * y/ Division x / y+ Addition x + y– Subtraction x – y
• A character string variable containing only numeric characters counts as a numeric .e.g. “22”
• Nonnumeric expressions are treated as 0 and generate a run-time warning
14
Concatenating Strings• The concatenation operator, : or Cats, links string
expressions to form compound string expressions.• Let X = “Tarzan”
Expression – "Hello. " : "My Name is " : X : ". What’s yours?“
• Evaluates to:– “Hello. My name is Tarzan. What’s yours?"
Expression – "There are " : "2" + "2" : "3" : " windows.“
• Evaluates to:– "There are 43 windows.“
15
Extracting Substrings
• A substring is a string within a string. [ ] operator to specify a substring
Syntax: string [ [ start, ] length ]• Trailing Substrings.
"1234567890" [5] returns the substring : 67890• Delimited Substrings.
string [ delimiter, instance, fields ]
• Assigning a Substring to a Variable.A="12345"A[3]=1212returns the result 121212.
16
Relational Operators
Operator Relation ExampleEq or = Equality X = YNe or # or >< or <> Inequality X # Y, X <> YLt or < Less than X < YGt or > Greater than X > YLe or <= or =< or #> Less than or equal to X <= YGe or >= or => or #< Greater than or equal to X >= Y
17
If Operators
• variable = If condition Then expression Else expression
If condition defines the condition that determines which value to assign.Then expression defines the value to assign if condition is true.Else expression defines the value to assign if condition is false.
18
Logical Operators
Numeric data, string data, and the null value can function as logical data:• The numeric value 0, is false; all other numeric
values are true.• An empty string is false; all other character
strings are true.• The SQL null value is neither true nor false. It
has the logical value of null.• And (or the equivalent &)• Or (or the equivalent !)
• Not (inverts a logical value)
19
Reserved Words
• And • Cat • Else •End• Eq • Ge • Get •
Go• GoSub • GoTo • If • Include• Le • Locked • Lt • Match• Matches • Ne • Next • Or• Rem • Remove• Repeat • Then• Until • While .GE
20
System Variables
21
System Variables
22
Assignment Operators
23
BASIC Functions and Statements
Compiler DirectivesCompiler directives are statements that determine how a routine or
transform is compiled.
To do this… Use this…• Add or replace an identifier $Define• Remove an identifier $Undefine• Specify conditional compilation $IfDef and $IfNDef• Include another program $Include
24
Compiler Directives-Example
• $Define identifier [replacement.text]• Example* Set next line to $UnDefine to switch off debugging code
$Define DebugMode...$IfDef DebugMode* In debugging mode, log each time through this routine.Call DSLogInfo("Transform entered,arg1 = ":Arg1, "Test")$EndIf
25
Job Control
• Used in a job control routine,
• Defined as part of a job’s properties
• Allows other jobs to be run and controlled from the first job
• Can be used for getting status information on the current job ,these are useful in active stage expressions and before- and after-stage subroutines.
26
Job Control
• To do this… Use this…• Specify the job you want to control DSAttachJob• Set parameters for the job you DSSetParam want to control• Set limits for the job you want to DSSetJobLimit control• Request that a job is run DSRunJob• Wait for a called job to finish DSWaitForJob• Get information about the current DSGetProjectInfo project• Get information about the controlled DSGetJobInfo job or current job• Get information about a stage in DSGetStageInfo the controlled job or current job• Get information about a link in DSGetLinkInfo a controlled job or current job
27
Program Control
To do this… Use this…
• Start a set of Case statements Begin Case• Specify conditions for program Case flow
• End a set of Case statements End Case• End a program or block of statements• Call an external subroutine Call• Call an internal subroutine GoSub• Specify a condition to call an On…GoSub internal subroutine• Return from an internal or Return external subroutine• Define the start of aFor…Next For loop• Define the end of a For…Next Next loop
28
Data Formatting
To do this… Use this…
• Convert data for output Oconv• Convert data on input Iconv• Format data for output Fmt• Format data by display position FmtDP
29
Example Case- End Case
30
Example Cats, Char
31
Example Convert ,Count
32
Example Date
33
Example For…. Next
Example
34
Example Gosub
Example
35
Example If ….Then….End
36
Example GoToExample GoTo
Example GoSub
37
Basic Rules
The main points to watch are as follows:• Do not use any command, function, statement, or
subroutine that requires any user input.• To stop a running job, use the DSLogFatal
subroutine. – if you use a Stop or Abort statement, the job
may be left in an irrecoverable condition.• Avoid using the Print statement. Use a call to
DSLogInfo to write to the job log file instead.• Avoid using the Execute statement to execute
dataStage Engine commands. Use a call to DSExecute instead.