Hi everyone,
I am fairly new to SAS and need your help. I have a dataset of school children with grades and food habits. Food habit section has five variables. each variable tells the no. of time one particular food is take. For example,
ID | Grade | Food A | Food B | Food C | Food D |
101 | 7 | Yes | No | Yes | Yes |
102 | 8 | No | No | No | Yes |
103 | 7 | Yes | Yes | No | Yes |
How can I make a table showing grade and food food habit?
Frequency | ||
Grade 7 | ||
Yes | 6 | |
No | 2 | |
Grade 8 | ||
Yes | 1 | |
No | 3 |
Would appreciate your help.
Thank you.
Khalid
Try this to generate desired output:
data have;
input ID Grade FoodA $ FoodB $ FoodC $ FoodD $;
datalines;
101 7 Yes No Yes Yes
102 8 No No No Yes
103 7 Yes Yes No Yes
;
proc transpose data=have out=want;
by id grade;
var FoodA FoodB FoodC FoodD;
run;
proc tabulate data=want;
class Grade col1;
table grade*col1=' ',n='Frequency';
run;
One way
proc tabulate data=have;
class grade fooda foodb foodc foodd;
table
grade *(fooda foodb foodc foodd),
n
;
run;
this does all the foods at once. Grade * fooda , n ; would do just fooda.
Try this to generate desired output:
data have;
input ID Grade FoodA $ FoodB $ FoodC $ FoodD $;
datalines;
101 7 Yes No Yes Yes
102 8 No No No Yes
103 7 Yes Yes No Yes
;
proc transpose data=have out=want;
by id grade;
var FoodA FoodB FoodC FoodD;
run;
proc tabulate data=want;
class Grade col1;
table grade*col1=' ',n='Frequency';
run;
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.