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

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

1 ACCEPTED SOLUTION

Accepted Solutions
Shmuel
Garnet | Level 18

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;

View solution in original post

3 REPLIES 3
Haikuo
Onyx | Level 15

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

Shmuel
Garnet | Level 18

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;

ballardw
Super User

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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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