The IF statement

Post questions here relative to DataStage Server Edition for such areas as Server job design, DS Basic, Routines, Job Sequences, etc.

Moderators: chulett, rschirm, roy

Post Reply
denzilsyb
Participant
Posts: 186
Joined: Mon Sep 22, 2003 7:38 am
Location: South Africa
Contact:

The IF statement

Post by denzilsyb »

Hallo

Which IF statement is recommended, bearing in mind that the correct results, performance and ability to understand is what we are after..

Code: Select all

if NOT(LINK_LOOKUP.NOTFOUND) and 
(
 trimb(LINK_ROLLUP.ORGTG_IBT_N) = 2610 or 
 trimb(LINK_ROLLUP.ORGTG_IBT_N) = 2710 or 
 trimb(LINK_ROLLUP.ORGTG_IBT_N) = 5810 or 
 trimb(LINK_ROLLUP.ORGTG_IBT_N) = 1409 or 
 trimb(LINK_ROLLUP.ORGTG_IBT_N) = 7526 or 
 trimb(LINK_ROLLUP.ORGTG_IBT_N) = 1426 or 
 trimb(LINK_ROLLUP.ORGTG_IBT_N) = 5837 or 
 trimb(LINK_ROLLUP.ORGTG_IBT_N) = 9405 or 
 trimb(LINK_ROLLUP.ORGTG_IBT_N) = 5745 or 
 trimb(LINK_ROLLUP.ORGTG_IBT_N) = 3145 
) then 
  LINK_ROLLUP.CASH_A + LINK_LOOKUP.CASH_DEP_R100_CC_A else 
LINK_ROLLUP.CASH_A
or...

Code: Select all

if NOT(LINK_LOOKUP.NOTFOUND) and trimb(LINK_ROLLUP.ORGTG_IBT_N) = 2610 then 
 LINK_ROLLUP.CASH_A + LINK_LOOKUP.CASH_DEP_R100_CC_A else if
NOT(LINK_LOOKUP.NOTFOUND) and trimb(LINK_ROLLUP.ORGTG_IBT_N) = 2710 then 
 LINK_ROLLUP.CASH_A + LINK_LOOKUP.CASH_DEP_R100_CC_A else if
NOT(LINK_LOOKUP.NOTFOUND) and trimb(LINK_ROLLUP.ORGTG_IBT_N) = 5810 then 
 LINK_ROLLUP.CASH_A + LINK_LOOKUP.CASH_DEP_R100_CC_A else if
NOT(LINK_LOOKUP.NOTFOUND) and trimb(LINK_ROLLUP.ORGTG_IBT_N) = 1409 then 
 LINK_ROLLUP.CASH_A + LINK_LOOKUP.CASH_DEP_R100_CC_A else if
NOT(LINK_LOOKUP.NOTFOUND) and trimb(LINK_ROLLUP.ORGTG_IBT_N) = 7526 then 
 LINK_ROLLUP.CASH_A + LINK_LOOKUP.CASH_DEP_R100_CC_A else if
NOT(LINK_LOOKUP.NOTFOUND) and trimb(LINK_ROLLUP.ORGTG_IBT_N) = 1426 then 
 LINK_ROLLUP.CASH_A + LINK_LOOKUP.CASH_DEP_R100_CC_A else if
NOT(LINK_LOOKUP.NOTFOUND) and trimb(LINK_ROLLUP.ORGTG_IBT_N) = 5837 then 
 LINK_ROLLUP.CASH_A + LINK_LOOKUP.CASH_DEP_R100_CC_A else if
NOT(LINK_LOOKUP.NOTFOUND) and trimb(LINK_ROLLUP.ORGTG_IBT_N) = 9405 then 
 LINK_ROLLUP.CASH_A + LINK_LOOKUP.CASH_DEP_R100_CC_A else if
NOT(LINK_LOOKUP.NOTFOUND) and trimb(LINK_ROLLUP.ORGTG_IBT_N) = 5745 then 
 LINK_ROLLUP.CASH_A + LINK_LOOKUP.CASH_DEP_R100_CC_A else if
NOT(LINK_LOOKUP.NOTFOUND) and trimb(LINK_ROLLUP.ORGTG_IBT_N) = 3145 then  
 LINK_ROLLUP.CASH_A + LINK_LOOKUP.CASH_DEP_R100_CC_A else 
LINK_ROLLUP.CASH_A
Which will bring back the correct results?
If they return different values different.. why?
Which is faster to interpret by DS? Is the second piec of code resulting in 11 reads (maximum) and the first set only resulting in 2 reads (maximum)?
dnzl
"what the thinker thinks, the prover proves" - Robert Anton Wilson
neena
Participant
Posts: 90
Joined: Mon Mar 31, 2003 4:32 pm

Post by neena »

Hi
Its not the question that which will run fast.. Its a question of which one is more effiecient way..
As my knowedge first one is easy to debug and easy to understand.
When ever you are building some logic .. keep it more simple... and more effective.. easy to understand by some others and easy to make some changes if there is any.
If you see ascential tips and tricks they clearly mentioned that "keep everthing simple" If you have a very complex situation .. devide that into number of pieces of work and work it.. This way you will have control where that is going wrong...

HTH.
Thanks,
Neena
chucksmith
Premium Member
Premium Member
Posts: 385
Joined: Wed Jun 16, 2004 12:43 pm
Location: Virginia, USA
Contact:

Post by chucksmith »

Combine the two concepts, and use stage variables.

Code: Select all

svTrimbOrgtgIbtN = trimb(LINK_ROLLUP.ORGTG_IBT_N)

Code: Select all

svAddItPrep = If  
 svTrimbOrgtgIbtN  = 2610 Then @TRUE Else If  
 svTrimbOrgtgIbtN  = 2710 Then @TRUE Else If  
 svTrimbOrgtgIbtN  = 5810 Then @TRUE Else If  
 svTrimbOrgtgIbtN  = 1409 Then @TRUE Else If  
 svTrimbOrgtgIbtN  = 7526 Then @TRUE Else If  
 svTrimbOrgtgIbtN  = 1426 Then @TRUE Else If  
 svTrimbOrgtgIbtN  = 5837 Then @TRUE Else If  
 svTrimbOrgtgIbtN  = 9405 Then @TRUE Else If  
 svTrimbOrgtgIbtN  = 5745 Then @TRUE Else If  
 svTrimbOrgtgIbtN  = 3145 Then @TRUE Else @FALSE

Code: Select all

svAddIt = If Not(LINK_LOOKUP.NOTFOUND) And svAddItPrep Then @TRUE Else @FALSE
Now in your derivation:

Code: Select all

If SvAddIt Then LINK_ROLLUP.CASH_A + LINK_LOOKUP.CASH_DEP_R100_CC_A Else 
LINK_ROLLUP.CASH_A
This is both clear and efficient.
chulett
Charter Member
Charter Member
Posts: 43085
Joined: Tue Nov 12, 2002 4:34 pm
Location: Denver, CO

Post by chulett »

You could change the derivation of the stage variable to make it a little easier - or at least what I think is a little easier on the eyes and brain. :wink: You don't need to explicitly set svAddItPrep to @TRUE or @FALSE:

Code: Select all

svTrimbOrgtgIbtN  = 2610 or svTrimbOrgtgIbtN  = 2710 or svTrimbOrgtgIbtN  = 5810 or svTrimbOrgtgIbtN  = 1409 or svTrimbOrgtgIbtN  = 7526 or svTrimbOrgtgIbtN  = 1426 or svTrimbOrgtgIbtN  = 5837 or svTrimbOrgtgIbtN  = 9405 or svTrimbOrgtgIbtN  = 5745 or svTrimbOrgtgIbtN  = 3145
This expression will evaluate to true or false and set the stage variable accordingly.
-craig

"You can never have too many knives" -- Logan Nine Fingers
kcbland
Participant
Posts: 5208
Joined: Wed Jan 15, 2003 8:56 am
Location: Lutz, FL
Contact:

Post by kcbland »

Or, you could create the INLIST function that I posted at
viewtopic.php?t=85538&postdays=0&postorder=asc&start=0
and make this really easy:

Code: Select all

if NOT(LINK_LOOKUP.NOTFOUND) and INLIST(trimb(LINK_ROLLUP.ORGTG_IBT_N), 2610,2710,5810,1409,7526,1426,5837,9405,5745,3145) then LINK_ROLLUP.CASH_A + LINK_LOOKUP.CASH_DEP_R100_CC_A else 
LINK_ROLLUP.CASH_A
Kenneth Bland

Rank: Sempai
Belt: First degree black
Fight name: Captain Hook
Signature knockout: right upper cut followed by left hook
Signature submission: Crucifix combined with leg triangle
denzilsyb
Participant
Posts: 186
Joined: Mon Sep 22, 2003 7:38 am
Location: South Africa
Contact:

Post by denzilsyb »

Thanks a lot guys!

This is the kind of response I was hoping for.
dnzl
"what the thinker thinks, the prover proves" - Robert Anton Wilson
rasi
Participant
Posts: 464
Joined: Fri Oct 25, 2002 1:33 am
Location: Australia, Sydney

Post by rasi »

Consider putting this into a reference table and use it as a hash lookup. This is maintenance free solution. Also not hard coded in your job. Anytime you need to add or change in the reference table. Worth considering it.

Thanks
Siva
vmcburney
Participant
Posts: 3593
Joined: Thu Jan 23, 2003 5:25 pm
Location: Australia, Melbourne
Contact:

Post by vmcburney »

I agree you should avoid hard coding these values in a transformer in case they need to be modified at regular intervals. A lookup table or even a routine would be easier. With a routine you just need to change the inlist and recompile the routine and the job will pick up the new values.
Post Reply