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

 Hello guys,

 

I have a data for 11 stress variables. Each item had 4 possible responses (1-4) with “4” indicating the worse stress. I am trying to write a code to print out all subjects who selected "4" for atleast 6 of the 11 items. The part in bold is where i've struggled to do.

Any correction will be appreciated.

 

data one; set mike;
array stress stress1-stress11;
count=0;
do over stress;
if stress = 4 then count= count+1;
end;

proc print; var stress1-stress11 count;
where count >=6;
run;

 

Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

Here is one way

 

data have(drop=i j);
   array stress{11};
   do i=1 to 100;
      do j=1 to dim(stress);
         stress[j]=rand('integer', 1, 4);
      end;
   output;
   end;
run;

data want(drop=i);
   set have;
   array stress{11};
   count=0;
   do i=1 to dim(stress);
      if stress[i]=4 then count=count+1;
   end;
run;

proc print data=want;
   where count ge 6;
run;

View solution in original post

4 REPLIES 4
PeterClemmensen
Tourmaline | Level 20

Here is one way

 

data have(drop=i j);
   array stress{11};
   do i=1 to 100;
      do j=1 to dim(stress);
         stress[j]=rand('integer', 1, 4);
      end;
   output;
   end;
run;

data want(drop=i);
   set have;
   array stress{11};
   count=0;
   do i=1 to dim(stress);
      if stress[i]=4 then count=count+1;
   end;
run;

proc print data=want;
   where count ge 6;
run;
ChuksManuel
Pyrite | Level 9

Thank you. That gave me another perspective on how to do the task.

Tom
Super User Tom
Super User

I don't see anything wrong with your code.  (Although you should add a RUN statement to end your data step.)

What problems are you actually having? If you are getting errors show the log (make sure to use the INSERT CODE icon on the editor menu to pop-up a window where you can paste the text from the log).

ChuksManuel
Pyrite | Level 9

Thank you! 

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1287 views
  • 0 likes
  • 3 in conversation