BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
daisy6
Quartz | Level 8

Hello,

I have a csv file:

 

Id      questionid                                              response                           answerkey                             grade   

123     q123|q234|q345|q456|q678|q789        A|B|C|D|A|B                      A|B|C|D|C|B                           Pass

......

......

 

I want the dataset like:

Id               questionid                            response                  answerkey                    grade

123            q123                                      A                                   A                               Pass

123            q234                                      B                                   B                               Pass

123            q345                                      C                                   C                              Pass

......

.....

 

Please help me out! I appreciate any help!

1 ACCEPTED SOLUTION

Accepted Solutions
daisy6
Quartz | Level 8

I already import the CSV file into SAS. Your solutions are the exactly what I want! Thank you very much for prompt reply!  

View solution in original post

11 REPLIES 11
ballardw
Super User

You aren't showing where your commas may actually appear.

Easiest would likely be to read the data as th3 5 variable you show then do something like this:

data want;
   set have;
   do i=1 to ( countw(questionid,'|'));
      question = scan(questionid,i,'|');
      responsevalue = scan(response,i,'|');
      key  = scan(answerkey,i,'|');
  end;
  keep id question responsevalue key grade;
run;

The new variables question, responsevalue and key are used instead of your existing variable names as doing something like:

 

questioned= scan(questioned,1,'|'); would overwrite the existing value. OR you could rename the old variables on the set statement with dataset options and use the previous variable names if that is critical.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Think you would need an output statement before the end;

PeterClemmensen
Tourmaline | Level 20

Use the datastep to import the data into SAS. A simple google seach will do the trick, tons of example thereof.

 

Onse it is there, do something like this

 

data have;
input Id$ questionid:$50. response:$20. answerkey:$20. grade$;
datalines;
123 q123|q234|q345|q456|q678|q789 A|B|C|D|A|B A|B|C|D|C|B Pass
;

data want(drop=i cnt questionid response answerkey);
   set have;
   cnt = countw(questionid, '|');
   i=1;
   do while(i < cnt);
      questionid_new = scan(questionid,i,'|');
      response_new = scan(response,i,'|');
      answerkey_new = scan(answerkey,i,'|');
      output;
      i = i+1;
   end;
run;
daisy6
Quartz | Level 8

I already import the CSV file into SAS. Your solutions are the exactly what I want! Thank you very much for prompt reply!  

daisy6
Quartz | Level 8

Another questions, If  some ID or candidates did not answer all the questions, and they only have 4 questionid and 4 response, how to deal with that situation?

 

PeterClemmensen
Tourmaline | Level 20

Since I haven't seen your data I can't be sure, but I think the solutions above can handle that situation.

daisy6
Quartz | Level 8

I checked my data, and your code works perfectly! 

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Two notes:

Please mark the post of the person who supplied you the correct answer as Correct.  This both links the correct answer to the original post, and gives credit to the solution provider.

 

Secondly, avoid closing one question and then starting another one in the same post - start a new topic.

daisy6
Quartz | Level 8
Thanks for the advice! RW9
ballardw
Super User

@daisy6 wrote:

Another questions, If  some ID or candidates did not answer all the questions, and they only have 4 questionid and 4 response, how to deal with that situation?

 


This probably should be in a new topic, referencing this one, depending on what you mean by "deal with". In the new topic provide some examples of different types of incomplete data and what you want the result to look like for them.

 

You may need to go further than just the data set but to the report(s) you may be wanting, which is why a new topic is preferred.

daisy6
Quartz | Level 8

Got it! Thanks for the advice! Thanks for solved my problem Ballardw!

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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
  • 11 replies
  • 932 views
  • 6 likes
  • 4 in conversation