DATA Step, Macro, Functions and more

Best way to convert verbal response choices to numeric values

Accepted Solution Solved
Reply
Contributor
Posts: 49
Accepted Solution

Best way to convert verbal response choices to numeric values

I have little experience with handling survey data with this many questions and that requires manipulation.  I have survey data with 38 questions and the responses are recorded in the dataset as Not at all, A little bit, Somewhat, Quite a bit,  and Very much.  I am trying to convert the responses to numeric scores so they can be summed up.  I would like the recode to be Not at all=0, A little bit=1, Somewhat=2, Quite a bit=3 and Very much=4. My Q1-38 are a numbered range so I would think I should be able to use the range during recoding. 

 

I am trying to avoid typing Q1-Q38 individually in an if then else statement.  I saw the "of" statement and thought it may be useful but it seems that the scenarios I have seen it used in are for mathematical operations.

 

Current format of the data:

patient ID VISIT Q1                  Q2             Q3             Q4               Q5....             Q38

1                1       somewhat     very much   a little bit   somewhat   quite a bit....   very much

1                2       a little bit     somewhat   very much   somewhat   quite a bit....   a little bit

.

.

.

450

 

I would like it to be:

patient ID VISIT Q1                  Q2             Q3             Q4               Q5....             Q38

1                1       2                     4                1                 2                3....                4

1                2       1                     2                4                2                 3...                 1

.

.

.

.

450

 

Does anyone have any suggestions on how I can handle this recoding? I am using SAS 9.4.  Please let me know if any additional information is needed.

 

Thanks.


Accepted Solutions
Solution
‎02-02-2017 10:41 AM
Super Contributor
Posts: 474

Re: Best way to convert verbal response choices to numeric values

[ Edited ]

Hi.

 

Could be done with a custom informat and arrays.

 

 proc format;
     invalue code (upcase)
             'NOT AT ALL'=0
             'LITTLE BIT'=1
             'SOMEWHAT'=2
             'QUITE A BIT'=3
             'VERY MUCH'=4
             other=-1;
run;

 

data want;
     set have;
     array Q {38} Q1-Q38;
     array R {38} R1-R38;
     do i = 1 to dim(Q);
        R(i)=input(Q(i),code.);
     end;
run;

 

Hope it helps.

 

Daniel Santos @ www.cgd.pt

View solution in original post


All Replies
Super User
Posts: 19,822

Re: Best way to convert verbal response choices to numeric values

You're trying to recode a variable and you can use either a format or IF/THEN.

 

Either way you should use an array to loop through all the variables. Create two arrays, one for the old values and one for the new values.

 

Here's some examples on how that might look:

http://www.ats.ucla.edu/stat/sas/seminars/SAS_arrays/

Contributor
Posts: 49

Re: Best way to convert verbal response choices to numeric values

thank you
Super User
Super User
Posts: 7,976

Re: Best way to convert verbal response choices to numeric values

Please post test data in the form of a datastep in future.  

This code is not tested as not typing that in:

data want (drop=q:);
  set have;
  array q{38};
  array nq{38} 8;
  do i=1 to 38;
    select(q{i});
      when ("Not at all") nq{i}=0;
      when ("A little bit") nq{i}=1;
      ...;
      otherwise;
    end;
  end;
run;
Contributor
Posts: 49

Re: Best way to convert verbal response choices to numeric values

My apologies for not posting test data as a datastep. Thank you for the code. I will try it out.
Solution
‎02-02-2017 10:41 AM
Super Contributor
Posts: 474

Re: Best way to convert verbal response choices to numeric values

[ Edited ]

Hi.

 

Could be done with a custom informat and arrays.

 

 proc format;
     invalue code (upcase)
             'NOT AT ALL'=0
             'LITTLE BIT'=1
             'SOMEWHAT'=2
             'QUITE A BIT'=3
             'VERY MUCH'=4
             other=-1;
run;

 

data want;
     set have;
     array Q {38} Q1-Q38;
     array R {38} R1-R38;
     do i = 1 to dim(Q);
        R(i)=input(Q(i),code.);
     end;
run;

 

Hope it helps.

 

Daniel Santos @ www.cgd.pt

Contributor
Posts: 49

Re: Best way to convert verbal response choices to numeric values

Posted in reply to DanielSantos
Thank you. Would you be able to clarify what dim does?
Super Contributor
Posts: 474

Re: Best way to convert verbal response choices to numeric values

[ Edited ]

Of course.

 

proc format is used to create a custom format to decode/transform specfic text into specific numbers.

 

For more info and examples, check the following paper of Lois Levin: http://www2.sas.com/proceedings/sugi30/001-30.pdf

 

Finally, to handle all those variable I've used two associative arrays (one for input, the other for output, since they are of different data types).

 

Q holds the input values, R holds the results which is Q transformed with the custom format.

 

i is just an auxilary variable to cycle from 1 to the total dimension of Q (= dim(Q) )

 

Here's another paper with some great info about arrays: http://www2.sas.com/proceedings/sugi30/242-30.pdf

 

Hope it helps.

 

Daniel Santos @ www.cgd.pt

Super User
Posts: 11,343

Re: Best way to convert verbal response choices to numeric values

Posted in reply to DanielSantos

Don't forget the corresponding DISPLAY format to show the text from the codes as needed. The reports at the end will likely want this somewhere.

☑ This topic is solved.

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

Discussion stats
  • 8 replies
  • 152 views
  • 1 like
  • 5 in conversation