Top Banner
1 Datastage – Basic Programming
37

Datastage – Basic Programming-1

Apr 07, 2015

Download

Documents

sam2sung2
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Datastage – Basic Programming-1

1

Datastage – Basic Programming

Page 2: Datastage – Basic Programming-1

2

Objective

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

Page 3: Datastage – Basic Programming-1

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

Page 4: Datastage – Basic Programming-1

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

Page 5: Datastage – Basic Programming-1

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)

Page 6: Datastage – Basic Programming-1

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.

Page 7: Datastage – Basic Programming-1

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.

Page 8: Datastage – Basic Programming-1

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.

Page 9: Datastage – Basic Programming-1

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.

Page 10: Datastage – Basic Programming-1

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.

Page 11: Datastage – Basic Programming-1

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.

Page 12: Datastage – Basic Programming-1

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

Page 13: Datastage – Basic Programming-1

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

Page 14: Datastage – Basic Programming-1

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

Page 15: Datastage – Basic Programming-1

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.

Page 16: Datastage – Basic Programming-1

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

Page 17: Datastage – Basic Programming-1

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.

Page 18: Datastage – Basic Programming-1

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)

Page 19: Datastage – Basic Programming-1

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

Page 20: Datastage – Basic Programming-1

20

System Variables

Page 21: Datastage – Basic Programming-1

21

System Variables

Page 22: Datastage – Basic Programming-1

22

Assignment Operators

Page 23: Datastage – Basic Programming-1

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

Page 24: Datastage – Basic Programming-1

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

Page 25: Datastage – Basic Programming-1

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.

Page 26: Datastage – Basic Programming-1

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

Page 27: Datastage – Basic Programming-1

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

Page 28: Datastage – Basic Programming-1

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

Page 29: Datastage – Basic Programming-1

29

Example Case- End Case

Page 30: Datastage – Basic Programming-1

30

Example Cats, Char

Page 31: Datastage – Basic Programming-1

31

Example Convert ,Count

Page 32: Datastage – Basic Programming-1

32

Example Date

Page 33: Datastage – Basic Programming-1

33

Example For…. Next

Example

Page 34: Datastage – Basic Programming-1

34

Example Gosub

Example

Page 35: Datastage – Basic Programming-1

35

Example If ….Then….End

Page 36: Datastage – Basic Programming-1

36

Example GoToExample GoTo

Example GoSub

Page 37: Datastage – Basic Programming-1

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.