BookmarkSubscribeRSS Feed
deleted_user
Not applicable
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.
2 REPLIES 2
SPR
Quartz | Level 8 SPR
Quartz | Level 8
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
Doc_Duke
Rhodochrosite | Level 12
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

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 1363 views
  • 0 likes
  • 3 in conversation