Help using Base SAS procedures

Help with Cleaning up a data set with Proc Transpose?

Reply
New Contributor
Posts: 4

Help with Cleaning up a data set with Proc Transpose?

I am working with an interesting data set, however I am having difficulty cleaning it up due to certain imported formatting issues I am encountering.

The data set is expanded with multiple columns indicating response to surveys. I am trying to condense the data set columns in SAS to have responses to each question unique to each column.I am a little confused on where to start for the coding. For instance:

Data set now:


Respondent ID    K   1   2  3   4 

1                      1  

2                               3

3                           2

4                       1

5                                       5

____________________

I would like to have

Respondent ID  Grade

1                      1

2                      3

3                      2

4                      1

5                      5

Any ideas?


I have attached the sample Excel document with the dataset for further details.


Thanks in advance!

PROC Star
Posts: 7,363

Re: Help with Cleaning up a data set with Proc Transpose?

You could just use the coalesce function.  E.g.:

data have;

  input id k _1 _2 _3;

  cards;

1 . . 3 .

2 1 . . .

3 . . . 4

;

data want (keep=id grade);

  set have;

  grade=coalesce(of k--_3);

run;

Super User
Posts: 5,083

Re: Help with Cleaning up a data set with Proc Transpose?

Another possibility:  read in the data yourself, in a DATA step, rather than importing it from Excel.  When you do that, omit the DSD option from the INFILE statement, so that multiple consecutive delimiters get treated as a single delimiter.  In that case, you could simply use:

input respondent_ID $ grade;

Good luck.

Super User
Posts: 9,681

Re: Help with Cleaning up a data set with Proc Transpose?

I like Art's code. Why do you have to use proc transpose ?

data have;
  input id k _1 _2 _3;
  cards;
1 . . 3 .
2 1 . . .
3 . . . 4
;
run;
proc transpose data=have out=want(drop=_name_ where=(col1 is not missing)) ;
by id;
var k _1 _2 _3;
run;

Ksharp

Super User
Posts: 10,500

Re: Help with Cleaning up a data set with Proc Transpose?

I'd say go back to SurveyMonkey (or other survey program, example sure looks like SurveyMonkey to me) and look at your data export options. There is one that provides one response per question instead of this format.

Ask a Question
Discussion stats
  • 4 replies
  • 225 views
  • 1 like
  • 5 in conversation