Greetings everyone,
I have an exam dataset for multiple response items but need to separate each option and transpose the dataset from long to wide then dummy code each option. Not all examinees answered all questions. Item1 and Item2 have 5 options (2 correct answers) while Item3 has 6 options (3 correct answers). How do I achieve this? Any help is much appreciated.
data exam;
input ID $ ItemID $ Response $;
cards;
1 Item1 BC
1 Item3 ACD
2 Item1 CD
2 Item2 AC
2 Item3 BCD
3 Item1 AB
3 Item2 BD
4 Item2 CE
4 Item3 ABF
5 Item1 AE
5 Item2 BD
5 Item3 BEF
;
data need;
input ID $ Item1A Item1B Item1C Item1D Item1E Item2A Item2B Item2C Item2D Item2E Item3A Item3B Item3C Item3D Item3E Item3F;
cards;
1 0 1 1 0 0 . . . . . 1 0 1 1 0 0
2 0 0 1 1 0 1 0 1 0 0 0 1 1 1 0 0
3 1 1 0 0 0 0 1 0 1 0 . . . . . .
4 0 0 1 0 1 . . . . . 1 1 0 0 0 1
5 1 0 0 0 1 0 1 0 1 0 0 1 0 0 1 1
;
Didn't we do this this the other day?
data exam;
infile cards missover;
input ID $ ItemID $ response $1. @;
do while(not missing(Response));
output;
input response $1. @;
end;
cards;
1 Item1 BC
1 Item3 ACD
2 Item1 CD
2 Item2 AC
2 Item3 BCD
3 Item1 AB
3 Item2 BD
4 Item2 CE
4 Item3 ABF
5 Item1 AE
5 Item2 BD
5 Item3 BEF
;;;;
run;
proc print;
run;
data examv/view=examv;
set exam(drop=id) exam;
run;
proc sort data=examv out=exam2;
by id itemid;
run;
proc summary data=exam2 nway completetypes classdata=exam;
by id itemid;
class response;
output out=exam3(drop=_type_);
run;
proc transpose data=exam3 out=want(drop=_name_ where=(not missing(id)));
by id;
var _freq_;
id itemid response;
run;
proc print;
run;
data want
data EXAM:
Didn't we do this this the other day?
data exam;
infile cards missover;
input ID $ ItemID $ response $1. @;
do while(not missing(Response));
output;
input response $1. @;
end;
cards;
1 Item1 BC
1 Item3 ACD
2 Item1 CD
2 Item2 AC
2 Item3 BCD
3 Item1 AB
3 Item2 BD
4 Item2 CE
4 Item3 ABF
5 Item1 AE
5 Item2 BD
5 Item3 BEF
;;;;
run;
proc print;
run;
data examv/view=examv;
set exam(drop=id) exam;
run;
proc sort data=examv out=exam2;
by id itemid;
run;
proc summary data=exam2 nway completetypes classdata=exam;
by id itemid;
class response;
output out=exam3(drop=_type_);
run;
proc transpose data=exam3 out=want(drop=_name_ where=(not missing(id)));
by id;
var _freq_;
id itemid response;
run;
proc print;
run;
data want
data EXAM:
Pay attention to the my EXAM data. It has one observation for each value of RESPONSE.
Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.
Register today!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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: