## Simple IF statement to compute new variable

Hello;

I currently have the following variables in my dataset.

c3r1 c3r2 c3r3 c3r4 c3r5 c3r6 c3r52 c3r56 c3ar1 c3ar2

I want to compute a new variable using the following rule:

1. If C3r1 or c3r2 or c3r3 ... c3r56 = 8 or 9 and c3ar1 = missing, then new variable = 99.

``````data temp;
set temp;
C3a_RB_r1= c3ar1;
if (c3r: = 8 or c3r: = 9) AND (c3ar1) = . then C3a_RB_r1= 99;

C3a_RB_r2= c3ar2;
if (c3r: = 8 or c3r: = 9) AND (c3ar2) = . then C3a_RB_r2= 99;run;``````

For the sake of demonstration, I have included few variables here. I'm trying to get a succinct code.

Please note that I want to rebase c3ar1 and c3ar2 in the example above. In my original dataset, I have c3ar1 to c3ar15, and so I would want to create 15 new variables c3a_RB_r1 to c3a_RB_r15. c3r: = 8 or 9 will be constant.

It would be great if I could get some help.

Tanks,

Neel

1 ACCEPTED SOLUTION

Accepted Solutions

## Re: Simple IF statement to compute new variable

Sounds like you are trying to use real numbers (8,9,99) to indicate missing values. The best solution is probably to first convert the special values into missing values (either regular missing or one of the 27 special missing codes).  Then you are just testing for the missing values, which functions like N(),NMISS() and CMISS() can help with.  Plus the missing values will be properly handled when you use those variables in other analysis steps.

You can probably do what you want with the WHICHN() function.

``````if (whichn(8,of c3r:) or whichn(9,of c3r:)) and missing(c3ar1) then C3a_RB_r1= 99;
``````
2 REPLIES 2

## Re: Simple IF statement to compute new variable

Sounds like you are trying to use real numbers (8,9,99) to indicate missing values. The best solution is probably to first convert the special values into missing values (either regular missing or one of the 27 special missing codes).  Then you are just testing for the missing values, which functions like N(),NMISS() and CMISS() can help with.  Plus the missing values will be properly handled when you use those variables in other analysis steps.

You can probably do what you want with the WHICHN() function.

``````if (whichn(8,of c3r:) or whichn(9,of c3r:)) and missing(c3ar1) then C3a_RB_r1= 99;
``````

## Re: Simple IF statement to compute new variable

Thank you so much! This helps a ton.
Discussion stats
• 2 replies
• 281 views
• 1 like
• 2 in conversation