BookmarkSubscribeRSS Feed
poulravn

Community Trekker

Joined:

Jan 25, 2012

How to extend the IF command in a formula? Equivalent of select;when;when;otherwise;end;

Hi, I want to be able, in a scripted formula, to assign a text string to column depending onthe values of another column.

For instance:

A  B

1  Low

2  Low

3  Low

4  Medium

5  Medium

6  Medium

7  High

8  High

9  High

10 High

How does the formula lok for this?  If(:A<=3,"Low",:A>=3 and :A<7,"Medium",:A>=7,"High") does not work.

I Base SAS there is a command, select(); when();when(); ; ; otherwise;end; It is the equivalent of this I am looking for.

Regards

Poul Ravn Sørensen

1 ACCEPTED SOLUTION

Accepted Solutions
Jeff_Perkinson

Community Manager

Joined:

Jun 23, 2011

Solution

Re: How to extend the IF command in a formula? Equivalent of select;when;when;otherwise;end;

Hi Poul,

You've got the right idea, just the wrong logical operator.

In JSL the AND operator is &. So, your formula should be:

     If(:A <= 3, "Low",

        :A >= 3 & :A < 7, "Medium",

        :A >= 7, "High")

-Jeff

-Jeff
5 REPLIES
scott_mitchell

Community Member

Joined:

Jul 7, 2013

Re: How to extend the IF command in a formula? Equivalent of select;when;when;otherwise;end;

You could use a format:

DATA HAVE;

DO A = 1 TO 10;

OUTPUT;

END;

RUN;

PROC FORMAT ;

VALUE RATING

LOW - 3  = "LOW"

4-6  = "MEDIUM"

7 - HIGH = "HIGH";

RUN;

DATA WANT;

  SET HAVE;

  B = PUT(A,RATING.);

RUN;

Or Using:

DATA WANT;

    ATTRIB C LENGTH = $6;

  SET HAVE;

/* B = PUT(A,RATING.);*/

      IF A <= 3 THEN C = "LOW";

  ELSE IF 4 <= A <= 6 THEN C = "MEDIUM";

  ELSE C = "HIGH";

RUN;

poulravn

Community Trekker

Joined:

Jan 25, 2012

Re: How to extend the IF command in a formula? Equivalent of select;when;when;otherwise;end;

I am sorry. This is the SAS program version, but I was trying to find the JMP Script formula for the same. Should have been more precise. Exactly this example shows how you would sometimes prefer to have a more comprehensive Scripting Guide. I found nothing to help me in the guide.

Regards

Poul

djchavda

Community Member

Joined:

Apr 8, 2013

Re: How to extend the IF command in a formula? Equivalent of select;when;when;otherwise;end;

Hi,

Try this

if A <= 3 then B="Low";

else if A > 3 and A <= 6 then B="Medium";

else B="High";

poulravn

Community Trekker

Joined:

Jan 25, 2012

Re: How to extend the IF command in a formula? Equivalent of select;when;when;otherwise;end;

Again I am sorry; this works fine in SAS but I am looking for the JMP Formula (based on IF, MATCH or CHOOSE?) that does the same.

Regards

Poul

Jeff_Perkinson

Community Manager

Joined:

Jun 23, 2011

Solution

Re: How to extend the IF command in a formula? Equivalent of select;when;when;otherwise;end;

Hi Poul,

You've got the right idea, just the wrong logical operator.

In JSL the AND operator is &. So, your formula should be:

     If(:A <= 3, "Low",

        :A >= 3 & :A < 7, "Medium",

        :A >= 7, "High")

-Jeff

-Jeff