Date: Sun, 20 Mar 2005 11:48:55 -0500
Reply-To: "Howard Schreier <hs AT dc-sug DOT org>" <nospam@HOWLES.COM>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: "Howard Schreier <hs AT dc-sug DOT org>" <nospam@HOWLES.COM>
Subject: Re: else if and if ? why they give different results ?
IF ... ELSE IF ... ELSE IF ... ELSE
sets up mutually exclusive actions. There can be any number if ELSE IF
statements in the "sandwich".
On the other hand, with
IF ... IF ... ELSE
the ELSE relates only to the immediately preceding IF. So ActorI and
ActorII will trigger both the first IF and the ELSE.
A SELECT structure can be used instead, and may be clearer.
On Sat, 19 Mar 2005 10:18:29 -0800, sawir yakup <ysawir@YAHOO.COM> wrote:
>Can someone explain why the codes below give different
>outputs when using if and else if statement ?
>
>data a;
>input jobcode $ count;
>cards;
>ActorI 2
>ActorII 3
>ActorIII 3
>ActorI 3
>. 4
>ActorIII 2
>ActorI 1
>ActorII 1
>ActorIII 2
>;
>/*proc freq; table jobcode; run; */
>
>/* this DATA step gives the right output */
>data b; set a;
> if jobcode in ('ActorI', 'ActorII') then
>joblevel='Beginner';
> else if jobcode = 'ActorIII' then
>joblevel='Advanced';
> else joblevel='Unknown';
> run;
>
>/* this DATA step gives the wrong output, why ? */
>data c; set a;
> if jobcode in ('ActorI', 'ActorII') then
>joblevel='Beginner';
> if jobcode = 'ActorIII' then joblevel='Advanced';
> else joblevel='Unknown';
> run;
>proc print data=b; title "Data B";
>proc print data=c; title "Data C"; run;
>
>
>
>__________________________________
>Do you Yahoo!?
>Yahoo! Small Business - Try our new resources site!
>http://smallbusiness.yahoo.com/resources/
|