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";
What if you have more than one fruit checked by a single person?
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.
you might want to look at catx function https://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a002257076.htm
and create fruits
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.
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;
@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.
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!
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.