Solved
New Contributor
Posts: 3

# array and do over question

I have a 10 item questionnaire in which all 10 questions have the same answer choices: “never”, ‘almost never”, “sometime”, “fairly often” and “very often”. I want to change the values in the dataset from text characters to number characters ranging from 0 to 4. However, I do not want to write 500 if/then statements. I need a code that searches all 10 questions finds the specified “text” and changes it to the corresponding number value.

I tried to do arrays and do over statements but I don’t know how to get the code just right. How can I modify the code below to produce what I want?  Thank you.

array b pss_oppera_q1-pss_oppera_q10;

do over b;

if b = "Never" then b = 0;

if b = "Almost never" then b = 1;

if b = "Sometimes" then b = 2;

if b = "Fairly often" then b = 3;

if b = "Very often" then b = 4;

end;

Accepted Solutions
Solution
‎10-03-2016 05:09 PM
Super User
Posts: 9,599

## Re: array and do over question

Not really on topic for the question, but you can replace the if syntax with a select for readability terms:

```array b...;
array c...;
do over b;
select(b);
when ("Never") c=0;
when ("Almost never") c=1;
when ("Sometimes") c=2;
when ("Fairly Often") c=3;
otherwise c=4;
end;
end;```

All Replies
Super User
Posts: 6,781

## Re: array and do over question

Assuming that you are actually replacing the original variables, those variables are character.  So you need to put the values in quotes:

do over b;

if b = "Never" then b = "0";

else if b = "Almost never" then b = "1";

else if b = "Sometimes" then b = "2";

else if b = "Fairly often" then b = "3";

else if b = "Very often" then b = "4";

end;

New Contributor
Posts: 3

## Re: array and do over question

I made a mistake in my original post, I didn't mean to say "number CHARACTER", I want to change these text values into number values.

Super User
Posts: 6,781

## Re: array and do over question

To save numeric values, you will need a new set of numeric variables:

array c num_q1 - num_q10;

Then inside the loop it will probably work to say:

do over b;

if b = "Never" then c = 0;

if b = "Almost never" then c = 1;

if b = "Sometimes" then c = 2;

if b = "Fairly often" then c = 3;

if b = "Very often" then c = 4;

end;

I can't test this right now, but it should work.

Super User
Posts: 13,563

## Re: array and do over question

Formats are another way.

``````proc format library=work;
invalue Response upcase
"NEVER"        = 0
"ALMOST NEVER" = 1
"SOMETIMES"    = 2
"FAIRLY OFTEN" = 3
"VERY OFTEN"   = 4
;
run;

/* in a data step*/
array b pss_oppera_q1-pss_oppera_q10;
array c num_q1 - num_q10;
do i=1 to dim(b);
c[i]= input(b[i],Response.);
end;``````

If your original data was read from a text file you could use the custom informat when reading that and start with the numeric values in the data.

Note: I used the Upcase option in case your example data may vary from the actual values by case of the letters.

Super User
Posts: 23,754

## Re: array and do over question

Or create a new variable rather than leave it as B.

Note that do over loops are deprecated but support remains for legacy.

Solution
‎10-03-2016 05:09 PM
Super User
Posts: 9,599

## Re: array and do over question

Not really on topic for the question, but you can replace the if syntax with a select for readability terms:

```array b...;
array c...;
do over b;
select(b);
when ("Never") c=0;
when ("Almost never") c=1;
when ("Sometimes") c=2;
when ("Fairly Often") c=3;
otherwise c=4;
end;
end;```
☑ This topic is solved.