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

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.

1 ACCEPTED SOLUTION

Accepted Solutions
DanielSantos
Barite | Level 11

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

8 REPLIES 8
Reeza
Super User

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/

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;
PaulaC
Fluorite | Level 6
My apologies for not posting test data as a datastep. Thank you for the code. I will try it out.
DanielSantos
Barite | Level 11

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

PaulaC
Fluorite | Level 6
Thank you. Would you be able to clarify what dim does?
DanielSantos
Barite | Level 11

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

ballardw
Super User

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.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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