Not applicable
Posts: 0

# If, then statements

Can anyone tell me how to do if, then, else statements using EG? I have tried several methods using the computed columns function and advanced expression builder, have tried the help option, have searched the forum, have several books, but none give instructions that seem to work with what I want to do. Other posts in this forum say to use the CASE function, but there is no such thing in the functions I can select and it hasn't worked when I've tried to use it. Essentially what I need to do is set up a variable that is:
If x=a and y=a, then z=1, else z=2.

Any suggestions?

Thanks.
Super Contributor
Posts: 366

## Re: If, then statements

Hello MonTanya,

You can do it but better to use direct coding it is equal in efforts. Anyway,
1. Open quiery builder for your dataset
2. Click on Add a New Computed Column
3. Sellect Advanced Expression and Click Next
4. In the field Build Expression write the following:

case when x=a and y=a then 1
else 2
end

5. Click Next
6. Enter z into Identifier field and click Next
7. Click Finish

Sincerely,
SPR
Posts: 2,127

## Re: If, then statements

There is a pretty straightforward logical expression to get that result for z in the advanced expression builder:

1*(x=a & y=a) + 2*(^(x=a & y=a))

"(x=a & y=a)" is a logical expression and always resolves to a 0 or 1, so the multiplication works cleanly.

If you need for Z to be missing if X, Y, or A are missing (and they are numeric variables), you can expand the expression:

1*(x=a & y=a) + 2*(^(x=a & y=a)) + 0*x*y

(For the case equivalent to an if-then-else in the EGuide Query Builder, you need to look at the documentation for the CASE expression in the SQL reference manual, not the CASE statement, as they are a bit different.)

Doc Muhlbaier
Duke
Discussion stats
• 2 replies
• 207 views
• 0 likes
• 3 in conversation