Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Else if statement

Status
Not open for further replies.

roland9

Technical User
Feb 8, 2006
17
US
Hi,

I was wondering as to how the solution to this problem is (B).Howcome Burt who has a level of 4 is assigned Medium..

Obs name level
1 Frank 1
2 Joan 2
3 Sui 2
4 Jose 3
5 Burt 4
6 Kelly .
7 Juan 1

The following SAS program is submitted:
data work.expertise;
set work.levels;
if level = . then
expertise = 'Unknown';
else if level = 1 then
expertise = 'Low';
else if level = 2 or 3 then
expertise = 'Medium';
else
expertise = 'High';
run;


Which of the following values does the variable EXPERTISE contain?
A. Low, Medium, and High only
B. Low, Medium, and Unknown only
C. Low, Medium, High, and Unknown only
D. Low, Medium, High, Unknown, and ' ' (missing character value)


Thanks
 
The reason is the Else..If statement that decides the medium choice will always return true. Well not exactly as missing and 1 will evaluate fine, but any value higher that 1 will get the medium setting.

The way to correct this is like this
Code:
else if level = 2 or
        level = 3 then
    expertise = 'Medium';

This is the correct way to write that statement.
Hope this has helped you,
Klaz
 
Hey Klaz,

I am sorry, being new to programing I still did not get it.What I understood that if level is 2 or 3 then expertise is 'medium'....(This statement should be true for all observations having a level of 2 or 3 )

Burt has a level of 4 which according to me falls in the High category.I thought anything that is not in 1 (Low),2 or 3 (Medium) is in (else expertise is 'high')

(Doesnt else if statement stop only if the condition is true.Please let me know I am sure I am missing out on something....)

Thanks
 
Roland9,
I corrected the statement that set the medium tag when level equals 2 or 3. The sas statement that you programed doesn't work.

Now, about the ELSE statement it shouldnt work the way you wrote it. The reason I can't remember off-hand but trust me it doesn't work. The way to make it work the way you envisioned the logic is to add the IF keyword to that ELSE statement (as an Else if). This way the logic will fall through until the last statement.
Hope that this helps you,
Klaz
 
Thanks for the help Klaz..Slowly getting to know some basics in programming..

Roland
 
have to think back as to what an If is evaluated into, either 0 (false) or 1 (true).

SAS treats any thing other then zero as a true conditions:

ie.. if 2 then somthing (Will always be true)

What is happening in the "else if level = 2 or 3 then" is first the Level = 2 is checked (false, since it is 4) or the number "3" (true).

So the 'or' portion is being executes and that is why you are getting the results of medium for Burt.

Does this help a little in the explaination?
 
Does this also work on the same level...


data work.joblevels;
set work.actors;
if jobcode in ('Actor I', 'Actor II') then
joblevel='Beginner';
if jobcode='Actor III' then
joblevel='Advanced';
else joblevel='Unknown';
run;
Which of the following represents the possible values for the variable joblevel in the Work.Joblevels data set?
a. Advanced and Unknown only
b. Beginner and Advanced only
c. Beginner, Advanced, and Unknown
d. ' ' (missing character value)

The answer is A.
 
I would choose a) Advanced and Unknown only.

The reason is simple:

First, SAS evaluates the first IF statement. Then it evaluates the 2nd IF statement (regardless of the previous results). The final Else only is evaluated in conjuction with the 2nd IF statement.
Therefore, you can only have the advanced or UNK for the value in joblevel.
Klaz
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top