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!
I already import the CSV file into SAS. Your solutions are the exactly what I want! Thank you very much for prompt reply!
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.
Think you would need an output statement before the end;
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;
I already import the CSV file into SAS. Your solutions are the exactly what I want! Thank you very much for prompt reply!
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?
Since I haven't seen your data I can't be sure, but I think the solutions above can handle that situation.
I checked my data, and your code works perfectly!
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 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.
Got it! Thanks for the advice! Thanks for solved my problem Ballardw!
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.
Ready to level-up your skills? Choose your own adventure.