Help using Base SAS procedures

If-then/else-if/else statements

Reply
N/A
Posts: 0

If-then/else-if/else statements

Hi,
I am trying to understand this, but always not getting to the key.Can any one help me with this..

/********PROGRAM********/
data 'C:\MySASLib\Old';
infile ' C:\MYSAS\groups.dat';
input Name $ 1-5 group 6;
run;
data New;
set 'C:\MySASLib\Old';
if group = . then new_grp = 'UNKNOWN';
else if group =1 then new_grp ='LOWER';
else if group=2 or 3 then new-grp='MEDIUM';
else new_grp= 'HIGHEST';
run;
proc print data= New;
run;
/***********END OF PROGRAM*************/


Q:-Now when I run this , what how is the value for new_grp variable only UNKNOWN, LOWER and MEDIUM??? Why isn't it considering the 'HIGH' value????
Below is the raw data file:groups.dat
Amie 1
Dolly 2
John 2
Eddie 3
Sue 4
Kieth 6
henry .
Jack 1

It would be really helpful, if any one cud reply to this as soon as possible.
Thanks.
N/A
Posts: 0

Re: If-then/else-if/else statements

Posted in reply to deleted_user
What happens if you change the last option from 'else' to 'else if group > 3' ?
Respected Advisor
Posts: 4,173

Re: If-then/else-if/else statements

Posted in reply to deleted_user
Hi SAS_Learner

The statement
else if group=2 or 3 then new-grp='MEDIUM';
always resolves to TRUE.

You could write:
else if group in(2,3) then new-grp='MEDIUM';


Another possibility would be to create a format (code not tested):

proc format;
value grpname
1 ='UNKNOWN'
2,3='LOWER'
>3 ='HIGHEST'
other='UNKNOWN'
;
run;

data 'C:\MySASLib\Old';
infile ' C:\MYSAS\groups.dat';
input Name $ 1-5 group 6;
run;
data New;
set 'C:\MySASLib\Old';
new_grp =put(group,grpname.);
run;

proc print data= New;
run;

HTH
Patrick
N/A
Posts: 0

Re: If-then/else-if/else statements

Hi Patrick,
I do agree that 'or' always resolves for TRUE, but then what about the groups higher than 3. Like for instance even for group 6 , it is showing medium.????
Of course I can get the correct , desired answer in many ways(which also includes the one u said PROC FORMAT).
But I wanted to know whats happening with this code (which I sent)??
Is it bcoz of using the 'or' operator , it no more considers any other values????

Thanks,
SAS_Learner
Super User
Posts: 5,424

Re: If-then/else-if/else statements

Posted in reply to deleted_user
I think you're missing the point here. Since SAS logic says that everything except the value of 0 resolves to TRUE, your OR 3 will always be TRUE, and therefore it will never go to your last ELSE statment, that will assign according to higher values than 2. So again, if you want your program to work, simply change your if statment to group in(2,3).

/Linus
Data never sleeps
Super Contributor
Posts: 291

Re: If-then/else-if/else statements

There is a coding problem here. You can't use group=2 or 3. One needs to use group=2 or group=3. Code below works as expected - same as if group in (2,3) in reply above.

if group = . then new_grp = 'UNKNOWN';
else if group =1 then new_grp ='LOWER';
else if group=2 or group=3 then new_grp='MEDIUM';
else new_grp= 'HIGHEST';
N/A
Posts: 0

Re: If-then/else-if/else statements

Bill,
I don't think there is any coding problem, because I wanted to know if I write in this way , how would SAS react.. and one of our frnds Linus H (as u can see other replies) gave the explanation to this.
But what you said , gives the correct output though.

Thanks,
SAS_Learner
N/A
Posts: 0

Re: If-then/else-if/else statements

YEAH, this is what I was expecting ...
Thank You Linus H.
That makes a lot of sense.

SAS_Learner
Ask a Question
Discussion stats
  • 7 replies
  • 158 views
  • 0 likes
  • 4 in conversation