Help using Base SAS procedures

IF-THEN-ELSE

Accepted Solution Solved
Reply
Contributor
Posts: 22
Accepted Solution

IF-THEN-ELSE

I am trying to redefine seven Likert scale questions into three groups in SAS 9.4, according to their overall "Likert score." Those who answered agree or strongly agree to all seven indicators score 28-35, those who answered disagree or strongly disagree score 7-14, and every other combination scores 15-27 (e.g. all agree and one disagree, all neutral, one of each, etc). When I run the following code, it codes every response as 15-27, even though my first four right off the bat should be 28-35. I have no idea what I could be doing wrong and would really appreciate some fresh eyes!

 

if (norm_full_imm='Agree' or norm_full_imm='Strongly Agree') & (imm_men_take='Agree' or imm_men_take='Strongly Agree') & (imm_resp_men='Agree' or imm_resp_men='Strongly Agree') & (imm_important='Agree' or imm_important='Strongly Agree') & (imm_safe='Agree' or imm_safe='Strongly Agree') & (imm_protect_kids='Agree' or imm_protect_kids='Strongly Agree') & (imm_heard_anything='Agree' or imm_heard_anything='Strongly Agree')
then Total_KAP="28-35";
else if (norm_full_imm='Disagree' or norm_full_imm='Strongly Disagree') & (imm_men_take='Disagree' or imm_men_take='Strongly Disagree') & (imm_resp_men='Disagree' or imm_resp_men='Strongly Disagree') & (imm_important='Disagree' or imm_important='Strongly Disagree') & (imm_safe='Disagree' or imm_safe='Strongly Disagree') & (imm_protect_kids='Disagree' or imm_protect_kids='Strongly Disagree') & (imm_heard_anything='Disagree' or imm_heard_anything='Strongly Disagree')
then Total_KAP="7-14"; else Total_KAP="15-27";

 

 

Originally I had

if (norm_full_imm='Agree' or 'Strongly Agree') &.....

but that didn't work either.

 

Thanks!


Accepted Solutions
Solution
‎02-21-2016 03:27 PM
Super User
Posts: 17,819

Re: IF-THEN-ELSE

Yes, then add them up. You should probably also consider arrays rather than manually looping through your variables. Here's a sample:

 

data want;
set have;
array orig(7) $ norm_full_imm imm_men_take imm_resp_men imm_important imm_safe imm_protect_kids imm_heard_anything;
array new(8) v1-v7;

do i=1 to 7;
if orig(i)='Strongly Agree' then new(i)=5;
else if orig(i)='Agree' then new(i)=5;
else if orig(i)='Neutral' then new(i)=3;
else if orig(i)='Disagree' then new(i)=2;
else if orig(i)='Strongly Disagree' then new(i)=1;
end;

score=sum(of new(*));

if 28 < score < 35 then total_kap='28-35';
else if 7 < score < 14 then total_kap='7-14';
else total_kap='15-27';


run;

View solution in original post


All Replies
Super User
Posts: 17,819

Re: IF-THEN-ELSE

Hmm...the comparisons are case sensitive.  You can check your data with a proc freq to see if the results are what you expect.

 

proc freq data=have;
table norm_full_imm*imm_men_take*imm_resp_men*imm_important*imm_safe*imm_protect_kids*imm_heard_anything;
run;

Also, you can clean your code a little bit as follows:

 

 

 

if 
(norm_full_imm in ('Agree' 'Strongly Agree')) & 
(imm_men_take in ('Agree' 'Strongly Agree')) & 
(imm_resp_men in ('Agree' 'Strongly Agree')) & 
(imm_important in ('Agree' 'Strongly Agree'))& 
(imm_safe in ('Agree' 'Strongly Agree'))& 
(imm_protect_kids in ('Agree' 'Strongly Agree')) & 
(imm_heard_anything in ('Agree' 'Strongly Agree'))
	then Total_KAP="28-35";
else if 
(norm_full_imm in ('Disagree' 'Strongly Disagree')) & 
(imm_men_take in ('Disagree' 'Strongly Disagree')) & 
(imm_resp_men in ('Disagree' 'Strongly Disagree')) & 
(imm_important in ('Disagree' 'Strongly Disagree')) & 
(imm_safe in ('Disagree' 'Strongly Disagree')) & 
(imm_protect_kids in ('Disagree' 'Strongly Disagree')) & 
(imm_heard_anything in ('Disagree' 'Strongly Disagree'))
	then Total_KAP="7-14"; 
else Total_KAP="15-27";

 

You haven't explained what you're trying to do, but I would likely suggest there's an easier way if you explain it in more detail. Sample data and expected output are very helpful.

 

Super User
Posts: 17,819

Re: IF-THEN-ELSE

Couldn't you also assign numeric scores to the variables, and then add them up to get your ranges? It looks like the categorization is based on the numeric values.

 

Also, check your cases - ie Strongly Agree and STRONGLY AGREE will not match and meet your IF conditions.

Contributor
Posts: 22

Re: IF-THEN-ELSE

@Reeza I'm not sure what you mean by assign a numeric value. Do you mean change "Strongly Agree" to "5" instead? 

Solution
‎02-21-2016 03:27 PM
Super User
Posts: 17,819

Re: IF-THEN-ELSE

Yes, then add them up. You should probably also consider arrays rather than manually looping through your variables. Here's a sample:

 

data want;
set have;
array orig(7) $ norm_full_imm imm_men_take imm_resp_men imm_important imm_safe imm_protect_kids imm_heard_anything;
array new(8) v1-v7;

do i=1 to 7;
if orig(i)='Strongly Agree' then new(i)=5;
else if orig(i)='Agree' then new(i)=5;
else if orig(i)='Neutral' then new(i)=3;
else if orig(i)='Disagree' then new(i)=2;
else if orig(i)='Strongly Disagree' then new(i)=1;
end;

score=sum(of new(*));

if 28 < score < 35 then total_kap='28-35';
else if 7 < score < 14 then total_kap='7-14';
else total_kap='15-27';


run;
Contributor
Posts: 22

Re: IF-THEN-ELSE

Ah, the array worked! Thank you so very much for your help (:

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 436 views
  • 0 likes
  • 2 in conversation