Contributor
Posts: 39

# Indicator Variable for Data Breakoff

Hello,

I am trying to create an indicator variable to filter some observations out and count how many observations I need. Basically, what I have is this:

data have=

1               99

1               22

1               53

1               99

1               99

1               99

2               34

2               57

2               99

2               99

2               99

2               99

What I want is for times when 99 is the last recorded answer to go back as far as the 99s go back to have a new variable mark it as a 1.

1               99              0

1               22              0

1               53              0

1               99              1

1               99              1

1               99              1

2               34              0

2               57              0

2               99              1

2               99              1

2               99              1

2               99              1

I know how to get a 1 for all of the 99s, but the part where it's only the last one going back I don't know how to get. Any help would be great.

Thanks.

Super User
Posts: 9,840

## Re: Indicator Variable for Data Breakoff

Posted in reply to jacob_klimek

I am not going to give working code as I am not typing that test data in - provide test data in the form of a datastep if you want working code.  The theory is quite simple however, proc sort the data so the data is in reverse order, then retain the indicator variable, then an if statement = 99 then set indicator to 1, and when not 99 set to zero again.

PROC Star
Posts: 1,400

## Re: Indicator Variable for Data Breakoff

[ Edited ]

@RW9 wouldn't that mean that the first occurence of answer being 99 will be given an indicator variable value of 1 when it is supposed to be zero?

Super User
Posts: 9,840

## Re: Indicator Variable for Data Breakoff

Have a secondary flag to show if the event has been triggered already and drop it.

PROC Star
Posts: 1,400

## Re: Indicator Variable for Data Breakoff

Ah yes of course, makes sense.

Regular Contributor
Posts: 164

## Re: Indicator Variable for Data Breakoff

Posted in reply to jacob_klimek
``````data have;
cards;
1 99
1 22
1 53
1 99
1 99
1 99
2 34
2 57
2 99
2 99
2 99
2 99
;
run;

data tmp;
set have;
n+1;
run;

proc sort data=tmp; by descending n id;run;

data tmp;
set tmp;
length indicator 8;
by descending id descending n;*assumes that id in have is provided in ascending sequence;
retain indicator;
if first.id and answer eq 99 then do;
indicator=1;
end;
if answer ne 99 then indicator=0;
run;

proc sort data=tmp out=tmp(drop=n); by n; run;

``````

Cheers

________________________

- Cheers -

Super User
Posts: 10,850

## Re: Indicator Variable for Data Breakoff

Posted in reply to jacob_klimek
```
So you only want indicator=1 when there are multiple 99s ?

data have;
cards;
1 99
1 22
1 53
1 99
1 99
1 99
2 34
2 57
2 99
2 99
2 99
2 99
;
run;
data want;
n=0;flag=0;
set have;
by id answer notsorted;
n+1;
end;
set have;
by id answer notsorted;
if answer=99 and n gt 1 then flag=1;
output;
end;
drop n;
run;

```
Discussion stats
• 6 replies
• 246 views
• 0 likes
• 5 in conversation