## Create new variable based on threshold

Occasional Contributor
Posts: 7

# Create new variable based on threshold

I need to combine multiple dichotomous variables into one but my code is not giving the correct output.

``````data cms_st2;
set cms_st;

If Eligible=1 then Status=2;
If Linked=0 and Eligible=0 then Status=0;

run;``````

The output this gives me is:

Status Frequency Cumulative
Frequency 0 1 2

 827 827 272 1099 493 1592

But Status=1 (i.e. Linked=1, regardless of whether Eligible=1 or 0) should be 765

Highlighted
Super User
Posts: 6,014

## Re: Create new variable based on threshold

Your statements execute in order.  So if you assign STATUS a value of 1, it's entirely possible that the next statement changes that and assigns STATUS a value of 2.  Perhaps this is what you meant for the first two statements:

else if Eligible=1 then Status=2;

Posts: 5,133

## Re: Create new variable based on threshold

is incorrect syntax.

PG
Super User
Posts: 12,148

## Re: Create new variable based on threshold

You really want to consider using an ELSE.

If your code was actually supposed to be :

If Eligible=1 then Status=2;
If Linked=0 and Eligible=0 then Status=0;

the result for Linked=1 Eligible=1 would be Status=2 because the second if is evaluated after the first assignment.

Perhaps:

ELSE If Eligible=1 then Status=2;
Else if Linked=0 and Eligible=0 then Status=0;

Occasional Contributor
Posts: 7

## Re: Create new variable based on threshold

when I run

``````data cms_st2;
set cms_st;

else If Eligible=1 then Status=2;
else If Linked=0 and Eligible=0 then Status=0;
run;``````

then it only assigns values for status 0 and 1, 2 is ignored.

Super User
Posts: 12,148

## Re: Create new variable based on threshold

This example code exists all the possibilities of dichotomous non-missing variables coded 0 and 1 and has results of 0, 1 and 2.

```data example;

else If Eligible=1 then Status=2;
else If Linked=0 and Eligible=0 then Status=0;
datalines;
0 0
1 0
0 1
1 1
;
run;```

If your are not getting 1 or 2 in your data then examine your log and see if you are getting any warnings

or run proc freq on your data:

Proc freq data=cms_st;