BookmarkSubscribeRSS Feed
Cindarellie
Calcite | Level 5

Hello!
I'm trying to create a variable that just pools all of the participants responses into one variable.

 

For example, there was a question that ask which following fruits do you eat? The participants can check off whether or not they eat each option, where 1=they checked it. So I want to create a variable where all the yes responses are in just one if that make senses.

 

When I run this code, I don't get any errors and the variable is created, however I noticed through PROC FREQ the frequency doesn't match the original variables-so I am wondering how to make sure it is pulling things correctly or what is happening for it to be getting the wrong number of observations?

 

I am not sure creating the variable using the if function is possible as that is most familiar method I use to create new variables. I am not familiar with arrays but would would that be better? 

 

 

 

if q_apple=1 then
fruits="apple";
else if q_orange=1 then
fruits="orange";
else if q_banana=1 then
fruits="banana";

else if q_grapes=1 then
fruits="grapes";

else if q_watermelon=1 then
fruits="watermelon";

else if q_strawberries=1 then
fruits="strawberries";

else if q_bb=1 then
fruits="blueberries";

else if q_ch=1 then
fruits="cherries";
else if q_other=1 then
fruits="Other";

7 REPLIES 7
Cindarellie
Calcite | Level 5
?I'm sorry I meant to say that the frequency changes

Amir
PROC Star

If the output of the proc freq is showing fewer observations then that might be because you have some missing values. The number of missing values might be displayed after all other frequencies, depending upon how you have chosen to write the proc freq code.

 

It would be helpful if you provided a complete sample data step with sample data that can be created like this and the proc freq code you have used.

 

 

Amir.

Astounding
PROC Star

If you want PROC FREQ to be able to count everything, your data set needs to contain everything.  Look at this code:

 

if q_apple=1 then
fruits="apple";
else if q_orange=1 then
fruits="orange";

It can only assign one value to FRUITS.  If more than one item has been checked, the program won't pick it up.  It ends up with APPLE or ORANGE, but not both.  Here's the sort of change you need to make before running PROC FREQ:

 

if q_apple=1 then do;
   fruits="apple";
   output;
end;
if q_orange=1 then do;
   fruits="orange";  
   output;
end;

That way, every selection becomes part of the data that feeds into PROC FREQ.  Do NOT use the word ELSE.

VDD
Ammonite | Level 13 VDD
Ammonite | Level 13
data want;
length fruits $256;
set have;
 
if q_apple=1 then
fruits=catx(" ", fruits, "apple");
if q_orange=1 then
fruits=catx(" ", fruits, "orange");
if q_banana=1 then
fruits=catx(" ", fruits, "banana");
if q_grapes=1 then
fruits=catx(" ", fruits, "grapes");
if q_watermelon=1 then
fruits=catx(" ", fruits, "watermelon");
if q_strawberries=1 then
fruits=catx(" ", fruits, "strawberries");
if q_bb=1 then
fruits=catx(" ", fruits, "blueberries");
if q_ch=1 then
fruits=catx(" ", fruits, "cherries");
if q_other=1 then
fruits=catx(" ", fruits, "Other");
run;
proc freq data=want;
tables fruits;
quit;


 

ballardw
Super User

@Cindarellie wrote:

Hello!
I'm trying to create a variable that just pools all of the participants responses into one variable.

 

For example, there was a question that ask which following fruits do you eat? The participants can check off whether or not they eat each option, where 1=they checked it. So I want to create a variable where all the yes responses are in just one if that make senses.

 

When I run this code, I don't get any errors and the variable is created, however I noticed through PROC FREQ the frequency doesn't match the original variables-so I am wondering how to make sure it is pulling things correctly or what is happening for it to be getting the wrong number of observations?

 

I am not sure creating the variable using the if function is possible as that is most familiar method I use to create new variables. I am not familiar with arrays but would would that be better? 

 

 

 

if q_apple=1 then
fruits="apple";
else if q_orange=1 then
fruits="orange";
else if q_banana=1 then
fruits="banana";

else if q_grapes=1 then
fruits="grapes";

else if q_watermelon=1 then
fruits="watermelon";

else if q_strawberries=1 then
fruits="strawberries";

else if q_bb=1 then
fruits="blueberries";

else if q_ch=1 then
fruits="cherries";
else if q_other=1 then
fruits="Other";


Show example starting data and what the result should look like. Otherwise there is lots of guessing as to what you want.

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
  • 7 replies
  • 1461 views
  • 0 likes
  • 6 in conversation