BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
lapetitemaman
Calcite | Level 5

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
;

1 ACCEPTED SOLUTION

Accepted Solutions
data_null__
Jade | Level 19

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

 

Capture.PNG

 data EXAM:

Capture.PNG

View solution in original post

3 REPLIES 3
data_null__
Jade | Level 19

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

 

Capture.PNG

 data EXAM:

Capture.PNG

lapetitemaman
Calcite | Level 5
Thank you for sharing your code. Somehow I was not able to get the data as shown. My actual ItemIDs are longer, more like I45609, I46874, etc. and they are not in sequential order (could this be the problem?). Somehow I got the item IDs listed as I45609AB, I45609ABC, I45609ABD, I45609ABE, etc. and the dummy code did not work correctly.
data_null__
Jade | Level 19

Pay attention to the my EXAM data.  It has one observation for each value of RESPONSE.

Ready to join fellow brilliant minds for the SAS Hackathon?

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!
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.

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
  • 3 replies
  • 458 views
  • 0 likes
  • 2 in conversation