## Use ARRAY to do recoding

Solved
Frequent Contributor
Posts: 110

# Use ARRAY to do recoding

[ Edited ]

Hi everyone,

I have a survey dataset. I am trying to use ARRAY to recode a series character variables VAR1-VAR5 into a series of numeric variables nVAR1-nVAR5 and a series of character variable rVAR1-rVAR5.Somehow I could only get nVAR1-nVAR5 , but not rVAR1-rVAR5. I always got the message "NOTE: Invalid numeric data".

data survey1;

set survey;

ARRAY VAR VAR1-VAR5;

ARRAY nVAR nVAR1-nVAR5;

ARRAY rVAR \$15 rVAR1-rVAR5;

DO OVER VAR;

IF VAR="Never" THEN DO;            nVAR =1; rVAR ="1 Never";         END;

ELSE IF VAR="Almost Never" THEN DO; nVAR =2; rVAR ="2 Almost Never"; END;

ELSE IF VAR="Sometimes" THEN DO;    nVAR =3; rVAR ="3 Sometimes";    END;

ELSE IF VAR="Fairly Often" THEN DO; nVAR =4; rVAR ="4 Fairly Often"; END;

ELSE IF VAR="Very Often" THEN DO;   nVAR =5; rVAR ="5 Very Often";   END;

END;

run;

Anyone has a suggestion for me? Thanks a lot!

Lizi

Accepted Solutions
Solution
‎10-24-2016 03:12 PM
Posts: 1,837

## Re: Use ARRAY to do recoding

you need a slight change:

data survey1;

set survey;

ARRAY VAR VAR1-VAR5;

ARRAY nVAR nVAR1-nVAR5;

LENGTH rVAR1-rVAR5 \$15;

ARRAY rVAR \$ rVAR1-rVAR5;

DO OVER VAR;

IF VAR="Never" THEN DO;            nVAR =1; rVAR ="1 Never";         END;

ELSE IF VAR="Almost Never" THEN DO; nVAR =2; rVAR ="2 Almost Never"; END;

ELSE IF VAR="Sometimes" THEN DO;    nVAR =3; rVAR ="3 Sometimes";    END;

ELSE IF VAR="Fairly Often" THEN DO; nVAR =4; rVAR ="4 Fairly Often"; END;

ELSE IF VAR="Very Often" THEN DO;   nVAR =5; rVAR ="5 Very Often";   END;

END;

run;

All Replies
Posts: 3,167

## Re: Use ARRAY to do recoding

One possility is that your input data 'survey' already has rVAR-rVar5, and they are defined as numeric.

Solution
‎10-24-2016 03:12 PM
Posts: 1,837

## Re: Use ARRAY to do recoding

you need a slight change:

data survey1;

set survey;

ARRAY VAR VAR1-VAR5;

ARRAY nVAR nVAR1-nVAR5;

LENGTH rVAR1-rVAR5 \$15;

ARRAY rVAR \$ rVAR1-rVAR5;

DO OVER VAR;

IF VAR="Never" THEN DO;            nVAR =1; rVAR ="1 Never";         END;

ELSE IF VAR="Almost Never" THEN DO; nVAR =2; rVAR ="2 Almost Never"; END;

ELSE IF VAR="Sometimes" THEN DO;    nVAR =3; rVAR ="3 Sometimes";    END;

ELSE IF VAR="Fairly Often" THEN DO; nVAR =4; rVAR ="4 Fairly Often"; END;

ELSE IF VAR="Very Often" THEN DO;   nVAR =5; rVAR ="5 Very Often";   END;

END;

run;

Super User
Posts: 13,577

## Re: Use ARRAY to do recoding

Much of the time when I see something like this I go back to the read step. Many variables of the same content practically beg for a custom informat to read the value "Never" as 1 and then a custom Format to display the value 1 as "1 Never". So then I force reading the data with the informat in a data step and assign the display format to the value. Then I only have one variable to deal with.

If I want to change the Displayed value then I need only create a new format, possibly one that will group 1 and 2 together and 4 and 5 together to create 3 levels for analyis with out adding yet another variable.

Note that most of the SAS analysis procedures will honor formats for grouping or creating categories.

☑ This topic is solved.

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

Discussion stats
• 3 replies
• 458 views
• 1 like
• 4 in conversation