BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
dude12345
Calcite | Level 5

I need to add a new column to a data set that would sort of be an if - then statement in excel how I know it.

I have a propensity column that calculates the propensity of certain diseases among age and gender groups.

What I want is to add a new column that returns a 1 if the propensity is greater than 0, and returns a 0 if propensity is 0.

Can anyone help me?

I saw A LOT of functions in the query builder "New Computed Column" --> "Advanced Expression" field. But I am not sure if that is it, I was originally thinking I would need to do a re coded column, but again I do not know for sure.

Any help would be appreciated.

THANKS!

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

There is more than one way.

A Case statement would also work in advanced expression. Its more like an if-then statement.

Case when propensity>0 then 1

          when propensity=0 then 0

          else -1

END

Remember to rename and re-alias the column in the dialog.

Here's a SQL reference to CASE statement.

CASE (Transact-SQL)

View solution in original post

5 REPLIES 5
Reeza
Super User

There is more than one way.

A Case statement would also work in advanced expression. Its more like an if-then statement.

Case when propensity>0 then 1

          when propensity=0 then 0

          else -1

END

Remember to rename and re-alias the column in the dialog.

Here's a SQL reference to CASE statement.

CASE (Transact-SQL)

Fugue
Quartz | Level 8

True true, the Case statement sitll works via "advanced expression". For some reason, SAS is promoting "if-then-else" logic via the "recode column" function. Both the "advanced expression" and the "recode column" features result in a CASE expression (when used for if-then-else logic).

dude12345
Calcite | Level 5

Thanks everyone! I am not at work anymore (and thus do not have access to our virtual machine running SAS)

I will try these out tomorrow.

RichardinOz
Quartz | Level 8

Another option to bear in mind for true/false conditions is the use of a function instead of case statements.

In this instance the sign() function would provide the result you require and is more efficient and less work than the case solution.

Richard

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!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 5 replies
  • 1353 views
  • 6 likes
  • 4 in conversation