0% found this document useful (0 votes)
1K views

Datastage - Basic Programming-1

This document provides an overview of basic programming concepts in Datastage including: - Constants, variables, functions, subroutines, operators, and system variables. It describes how to use dimensional arrays, concatenate strings, extract substrings, and perform logical operations. - Job control statements allow controlling and getting status of other jobs. Compiler directives include $Define, $IfDef, and $Include. - Examples demonstrate using case statements, formatting output, date functions, and flow control with GoSub, If/Then, and For/Next loops. Basic programming rules prohibit user input.

Uploaded by

sam2sung2
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1K views

Datastage - Basic Programming-1

This document provides an overview of basic programming concepts in Datastage including: - Constants, variables, functions, subroutines, operators, and system variables. It describes how to use dimensional arrays, concatenate strings, extract substrings, and perform logical operations. - Job control statements allow controlling and getting status of other jobs. Compiler directives include $Define, $IfDef, and $Include. - Examples demonstrate using case statements, formatting output, date functions, and flow control with GoSub, If/Then, and For/Next loops. Basic programming rules prohibit user input.

Uploaded by

sam2sung2
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 37

Datastage – Basic Programming

1
Objective
• Constant , Variables
• Functions
• Subroutines
• Operators
• Reserved Words
• System Variables
• Compiler Directives
• Job Control
• Examples
• Basic Rules

2
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

3
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..

4
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)

5
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.

6
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.

7
Labels
• Statement Labels -

– unique identifier for a line of code.


– Can be put either in front of a statement or on its own line.

8
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.

9
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.

10
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.

11
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

12
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

13
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.“

14
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]=1212
returns the result 121212.

15
Relational Operators

Operator Relation Example


Eq or = Equality X=Y
Ne or # or >< or <> Inequality X # Y, X <> Y
Lt or < Less than X<Y
Gt or > Greater than X>Y
Le or <= or =< or #> Less than or equal to X <= Y
Ge or >= or => or #< Greater than or equal to X >= Y

16
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.

17
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)

18
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

19
System Variables

20
System Variables

21
Assignment Operators

22
BASIC Functions and Statements

Compiler Directives
Compiler 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

23
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

24
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.

25
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

26
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

27
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

28
Example Case- End Case

29
Example Cats, Char

30
Example Convert ,Count

31
Example Date

32
Example For…. Next
Example

33
Example Gosub
Example

34
Example If ….Then….End

35
Example GoTo
Example GoTo

Example GoSub

36
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.

37

You might also like