Using Arrays

Reply
Contributor
Posts: 30

Using Arrays

Hello.

I have data that looks like this:

DATA HAVE:

A1     A2      A3      A4

1         1        2        3      

2         4        5        1

4         5        2        3

4         2        3        2

5         5        4        5

1         2        3        4


I would like a variable corresponding to each previous variable to indicate whether or not it is a 4 or a 5.

DATA WANT:

A1      P1

1         0        

2         0 

4         1 

4         1 

5         1 

1         0 

And I would like this for each variable.  Right now I have an array set up, but for some reason it is not doing what I thought it would.  Any help is greatly appreciated.

data as;

     set hosp;

     array a_questions (18) a1-a18;

     array a_positive (18) p1-p18;

     do i = 1 to 18;

          if a_questions = 4 or a_questions = 5 then a_positive = 1;

          else a_positive = 0;

     end;

run;

Super User
Posts: 17,960

Re: Using Arrays

It works for me, so there's something else going on. Are all your variables numeric?

Can you post your log?

Super User
Posts: 10,550

Re: Using Arrays

You might tell us in what way it isn't behaving as expected. I might guess that if any of your A variables are missing a value that you do not want the P value assigned. If that is the case then you want to only do the assignment when the variable isn't missing.

The IN operator is easer to read and update for enumerated values either numeric or string so you don't have to write a bunch of comparisons in your if statement.

BTW, An equivalent code for the assignment you are currently using would be:

a_positive = (a_questions in (4,5));

OR

If not missing(a_questions) then a_positive = (a_questions in (4,5));

Contributor
Posts: 30

Re: Using Arrays

Thank you for your help.  I ran the code I already had today and it seemed to work fine.  I have no idea what happened. 

Ask a Question
Discussion stats
  • 3 replies
  • 291 views
  • 2 likes
  • 3 in conversation