DATA Step, Macro, Functions and more

Regarding CASE statement in BASE SAS

Reply
N/A
Posts: 0

Regarding CASE statement in BASE SAS

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
Super Contributor
Posts: 394

Re: Regarding CASE statement in BASE SAS

Posted in reply to deleted_user
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.
N/A
Posts: 0

Re: Regarding CASE statement in BASE SAS

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
Super Contributor
Posts: 394

Re: Regarding CASE statement in BASE SAS

Posted in reply to deleted_user
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.
N/A
Posts: 0

Re: Regarding CASE statement in BASE SAS

Bingo!!!!!!!!

The IFC function is perfectly working for my requirements Smiley Happy . 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 Smiley Happy

Kapil
Frequent Contributor
Posts: 95

Re: Regarding CASE statement in BASE SAS

Posted in reply to deleted_user
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).
Trusted Advisor
Posts: 2,116

Re: Regarding CASE statement in BASE SAS

Posted in reply to deleted_user
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
Ask a Question
Discussion stats
  • 6 replies
  • 680 views
  • 0 likes
  • 4 in conversation