BookmarkSubscribeRSS Feed
deleted_user
Not applicable
Hello SAS Gurus,

I am using a DATA STEP where I want to write an expression for a variable in my dataset. In that expression, I want to evaluate one condition. If that condition is TRUE, then need to assign and if its FALSE then another value .

DATA
SET
VAR1 = ?if TRUE then : if FALSE then ;
END;
RUN;

When searched on SAS help, I got to know about CASE statement where we can write a condition in an expression. But unfortunately its not working and giving some syntax error.

NOTE: The JAVA equivalent to such requirement is to use a ternary operator something like this:

VAR1= ? :

Value1 will be assigned to VAR1 if cond2 is true, and value2 in case of FALSE.

Need to find the SAS equivalent to the above.
Request you to suggest the correct syntax or any other way to achieve the above requirement.

Any help regarding this would be highly appreciated.
Thanks in Advance.

regards
Kapil Agrawal
6 REPLIES 6
Tim_SAS
Barite | Level 11
Kapil,

The simplest solution is to use an IF statement:

[pre]
IF THEN
VAR1 = ;
ELSE
VAR1 = ;
[/pre]

Does this meet your needs? The data step language does not have the ?: ternary operator.
deleted_user
Not applicable
Hi Tim.
Thanks for the reply.

It will work, no doubt. But due to some reasons, I want to assign the value to the variable through expression only (need to use in SAS ETL) and thats where the problem is.

Off course what you have suggested is the final workaround for it; but as long as possible I want to build an expression for a variable where I can evaluate the condition and assign corresponding value.

Kapil
Tim_SAS
Barite | Level 11
How about the IFC and IFN functions?

Here's the 9.1 doc for IFC: http://support.sas.com/onlinedoc/913/getDoc/en/lrdict.hlp/a002604570.htm.
deleted_user
Not applicable
Bingo!!!!!!!!

The IFC function is perfectly working for my requirements 🙂 . Infact its serving the same wot I was struggling for.

Thanks a lot Tim.

Really appreciate your knowledge and skills in SAS.

Once again thnx for all the help 🙂

Kapil
1162
Calcite | Level 5
By the way, I think the CASE statement is specific to PROC SQL and doesn't work in DATA step processing (as you've already discovered).
Doc_Duke
Rhodochrosite | Level 12
Another way is with an assignment statement (this assigns a 1 for true and a 0 for false):

var1 = ();

where is any logical expression.

If all the variables involved in are numeric, you can extend that and propagate missing values from the variables in . Assume involves VarA, VarB, and VarC, then

var1 = () + 0*vara*varb*varc;

returns 1 for true, 0 for false, and . if any of the condition variables are missing.

This works because logical expressions always resolve to 0 or 1 and arithmetic expressions propagate missing values in SAS.

Doc Muhlbaier
Duke

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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