## Creating a binary variable from consecutive values from different variables

Occasional Contributor
Posts: 5

# Creating a binary variable from consecutive values from different variables

Hello I am not sure if someone could help me with the following:

I have a longitudinal dataset of a binary variable that  is equal to 1 when there is an infection in a patient, otherwise it is 0. Sometimes infections persist for seveal visits, sometimes they only remain for one visit and the patient is cured.I want to create 3 variables (X!, X2, X3) that would indicate a 1 or a 0 according to the following criteria:

In other words, this is how is might look and what I which to have my program do:

ID visit1  visit2  visit3  visit4      VARIABLE X1     VARIABLE X2         VARIABLE X3

1     0      0          0          0               0                              0                              0

2     0      1          0          0               0                              1                              0

3     1     1           0          0               1                              1                              0

4     0      1          1          1                1                              1                             1

5    0       1          0          1               0                               1                              0

X1) Variable is = 1 when there is an infection for At least two or more consecutive visits per patient.

X2) Variable is = 1 when there is an infection for At least one visits per patient (Any infection).

X3) Variable is = 1 when there is an infection for At least three or more consecutive visits per patient.

Anyone can help me? I do not know how to solve this.

Thank youse

Posts: 3,852

## Re: Creating a binary variable from consecutive values from different variables

Something like this perhaps.

data visit;
input id v1-v4;
length r \$4;
r=cats(of v;
x1=not not find(r,
'11');
x2=not not find(r,'1');
x3=not not find(r,'111');
cards;
1 0 0 0 0
2 0 1 0 0
3 1 1 0 0
4 0 1 1 1
5 0 1 0 1
;;;;
run;
proc print;

run;
Regular Contributor
Posts: 168

## Re: Creating a binary variable from consecutive values from different variables

How does 'not not find' works here? Is it a function by any chance?

Posts: 3,852

## Re: Creating a binary variable from consecutive values from different variables

NOT is a Boolean operator, in this case NOT NOT turns the position returned by FIND into 1 or ZERO making a Boolean of it.