BookmarkSubscribeRSS Feed
Katie
Obsidian | Level 7

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;

3 REPLIES 3
Reeza
Super User

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

Can you post your log?

ballardw
Super User

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));

Katie
Obsidian | Level 7

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. 

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!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 604 views
  • 2 likes
  • 3 in conversation