BookmarkSubscribeRSS Feed
deleted_user
Not applicable
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.
7 REPLIES 7
deleted_user
Not applicable
What happens if you change the last option from 'else' to 'else if group > 3' ?
Patrick
Opal | Level 21
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
deleted_user
Not applicable
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
LinusH
Tourmaline | Level 20
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
Bill
Quartz | Level 8
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';
deleted_user
Not applicable
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
deleted_user
Not applicable
YEAH, this is what I was expecting ...
Thank You Linus H.
That makes a lot of sense.

SAS_Learner

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 7 replies
  • 808 views
  • 0 likes
  • 4 in conversation