turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- Base SAS Programming
- /
- Regarding CASE statement in BASE SAS

Topic Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

08-26-2008 07:46 AM

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

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

DATA

SET

VAR1 =

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

08-26-2008 08:16 AM

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.

The simplest solution is to use an IF statement:

[pre]

IF

VAR1 =

ELSE

VAR1 =

[/pre]

Does this meet your needs? The data step language does not have the ?: ternary operator.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

08-26-2008 08:38 AM

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

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

08-26-2008 09:38 AM

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.

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

08-27-2008 03:28 AM

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

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

08-27-2008 12:56 PM

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).

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

08-26-2008 11:51 AM

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

var1 = (

where

If all the variables involved in

var1 = (

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