DATA Step, Macro, Functions and more

how to read csv files with some variabls delimited with bars into sas?

Accepted Solution Solved
Reply
Highlighted
Contributor
Posts: 45
Accepted Solution

how to read csv files with some variabls delimited with bars into sas?

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!


Accepted Solutions
Solution
Thursday
Contributor
Posts: 45

Re: how to read csv files with some variabls delimited with bars into sas?

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


All Replies
Super User
Posts: 11,104

Re: how to read csv files with some variabls delimited with bars into sas?

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.

Super User
Super User
Posts: 7,681

Re: how to read csv files with some variabls delimited with bars into sas?

Think you would need an output statement before the end;

PROC Star
Posts: 653

Re: how to read csv files with some variabls delimited with bars into sas?

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;
Solution
Thursday
Contributor
Posts: 45

Re: how to read csv files with some variabls delimited with bars into sas?

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

Contributor
Posts: 45

Re: how to read csv files with some variabls delimited with bars into sas?

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?

 

PROC Star
Posts: 653

Re: how to read csv files with some variabls delimited with bars into sas?

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

Contributor
Posts: 45

Re: how to read csv files with some variabls delimited with bars into sas?

I checked my data, and your code works perfectly! 

Super User
Super User
Posts: 7,681

Re: how to read csv files with some variabls delimited with bars into sas?

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.

Contributor
Posts: 45

Re: how to read csv files with some variabls delimited with bars into sas?

Thanks for the advice! RW9
Super User
Posts: 11,104

Re: how to read csv files with some variabls delimited with bars into sas?


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.

Contributor
Posts: 45

Re: how to read csv files with some variabls delimited with bars into sas?

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

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 11 replies
  • 113 views
  • 6 likes
  • 4 in conversation