BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
chelsealutz
Fluorite | Level 6

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!

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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

5 REPLIES 5
Reeza
Super User

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.

 

Reeza
Super User

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.

chelsealutz
Fluorite | Level 6

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

Reeza
Super User

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;
chelsealutz
Fluorite | Level 6

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

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 5 replies
  • 1353 views
  • 0 likes
  • 2 in conversation