## Recoding continuous variables into an array of binary variables

Solved
Occasional Contributor
Posts: 11

# Recoding continuous variables into an array of binary variables

Hi, I am relatively new to SAS and stuck with a problem. For a certain type of time-to-event analysis, I need to create a new dataset from the one that I'm working with. In the original dataset, each study participant has one row of data. Assume we have a patient X who has been in our study for 5 years and has developed the event of interest in the fourth year of the study. I need to create an array for this person T1 through T5 that has the values [0 0 0 1 1], respectively. That is zeros for the three years without the event and ones for the years 4 and 5 that the event occurred. I need a way to figure out how to code this in SAS so that the system automatically does this procedure for some 60,000 patients in the dataset. Each patient has a different follow-up time [1-24 years], may or may not have experienced the event [0 and 1] and may have the event of interest at any point in time during the follow-up. Can somebody please suggest how should I go about this problem?

Accepted Solutions
Solution
‎04-10-2017 01:01 PM
PROC Star
Posts: 1,283

## Re: Recoding continuous variables into an array of binary variables

Like this?

``````data work.sample;
input ID followup event event_occur;
datalines;
1 8  0 .
2 15 1 9
3 3  0 .
4 8  0 .
5 12 1 5
;

proc sort data = sample;
by descending followup;
run;

data _NULL_;
set sample;
if _N_=1 then call symput('cols',followup);
run;

proc sort data = sample;
by ID;
run;

data want;
set sample;

array period[&cols];

i = 1;
do until (i > followup);
period[i] = 0;
i + 1;
end;

if not missing(event_occur) then do j = event_occur to followup;
period[j] = 1;
end;

drop i j;
run;

``````

All Replies
PROC Star
Posts: 1,283

## Re: Recoding continuous variables into an array of binary variables

Can you provide some sample data?

Occasional Contributor
Posts: 11

## Re: Recoding continuous variables into an array of binary variables

Hi,

This is a very simple dataset of what I have in mind:

```data work.sample;
input ID followup event event_occur;
datalines;
1 8 0 .
2 15 1 9
3 3 0 .
4 8 0 .
5 12 1 5
run;```

There are four variables. ID indicates patient's unique ID. followup is the total amount of time in years that the person has been in the study. The variable event indicates whether the event of interest has happened. Finally, for patients with event=1, there is a time called event_occured that shows at which year the event has occurred. For instance in patient #2, she has been in the study for 15 years and then the event of interest occurred in year 9. The Binary array for her should be: 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1

On the other hand, patient #1 should have the following binary code of 8 zeros: 0 0 0 0 0 0 0 0

PROC Star
Posts: 1,283

## Re: Recoding continuous variables into an array of binary variables

And you want this in a dataset? If I understand you correctly, then in this case you want an output dataset with 15 additional variables (max(followup)) with 0/1?

Occasional Contributor
Posts: 11

## Re: Recoding continuous variables into an array of binary variables

Exactly! The new dataset would have additional variables equal to the maximum of the follow-up years.
Solution
‎04-10-2017 01:01 PM
PROC Star
Posts: 1,283

## Re: Recoding continuous variables into an array of binary variables

Like this?

``````data work.sample;
input ID followup event event_occur;
datalines;
1 8  0 .
2 15 1 9
3 3  0 .
4 8  0 .
5 12 1 5
;

proc sort data = sample;
by descending followup;
run;

data _NULL_;
set sample;
if _N_=1 then call symput('cols',followup);
run;

proc sort data = sample;
by ID;
run;

data want;
set sample;

array period[&cols];

i = 1;
do until (i > followup);
period[i] = 0;
i + 1;
end;

if not missing(event_occur) then do j = event_occur to followup;
period[j] = 1;
end;

drop i j;
run;

``````
Occasional Contributor
Posts: 11

## Re: Recoding continuous variables into an array of binary variables

Oh my god! Yes! Exactly. Thank you so very much! Appreciate your help and time!
PROC Star
Posts: 1,283