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
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;
One possility is that your input data 'survey' already has rVAR-rVar5, and they are defined as numeric.
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;
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.
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.