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.

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 925 views
  • 0 likes
  • 2 in conversation