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

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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