☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Pyrite | Level 9

## how to keep values in the following years once specified?

Hi everyone 🙂

I make a simple example what I want to make

I have a dataset like...

ID Year   V1 V2

1  2002   0

2  2000   0

2  2001   1

2  2002   0

3  2005   0

3  2006   1

3  2007   1

What I am trying to do is to create V2s equal to 1 if V1s grouped by ID and Year hit 1, or 0 otherwise + if V2 is first set as 1 then V2s in the subsequent years become 1 as well regardless of the value of V1s.  <---this is a tricky part. As shown below, I want to keep V2 in the following years after V2s are set as 1.

What I am doing is to make this... there is a threshold and if an observation hits this threshold then this observation is specified as something after all.

I owe a lot to this community and appreciate your help tremendously!   🙂

ID Year   V1 V2

1  2002   0    0

2  2000   0    0

2  2001   1    1

2  2002   0    1

3  2005   0    0

3  2006   1    1

3  2007   0    1

1 ACCEPTED SOLUTION

Accepted Solutions
Super User

## Re: how to keep values in the following years once specified?

A simple application of retaining a variable.

Note that the variable cannot be on the input (otherwise the retained value is replaced when the next record is read by the SET statement).

``````data have;
input ID Year V1 ;
cards;
1 2002 0
2 2000 0
2 2001 1
2 2002 0
3 2005 0
3 2006 1
3 2007 1
;

data want;
set have;
by id year ;
if first.id then v2=v1;
v2=max(v2,v1);
retain v2;
run;``````

Result

```Obs    ID    Year    V1    v2

1      1    2002     0     0
2      2    2000     0     0
3      2    2001     1     1
4      2    2002     0     1
5      3    2005     0     0
6      3    2006     1     1
7      3    2007     1     1
```
2 REPLIES 2
Tourmaline | Level 20

## Re: how to convert values in a column if this value hits a certain threshold

``````data have;
input ID Year V1;
datalines;
1 2002 0
2 2000 0
2 2001 1
2 2002 0
3 2005 0
3 2006 1
3 2007 1
;

data want;
set have;
by ID;
if first.ID then V2 = 0;
if V1 then V2 = 1;
retain V2;
run;``````

Result

```ID  Year  V1  V2
1   2002  0   0
2   2000  0   0
2   2001  1   1
2   2002  0   1
3   2005  0   0
3   2006  1   1
3   2007  1   1```

Super User

## Re: how to keep values in the following years once specified?

A simple application of retaining a variable.

Note that the variable cannot be on the input (otherwise the retained value is replaced when the next record is read by the SET statement).

``````data have;
input ID Year V1 ;
cards;
1 2002 0
2 2000 0
2 2001 1
2 2002 0
3 2005 0
3 2006 1
3 2007 1
;

data want;
set have;
by id year ;
if first.id then v2=v1;
v2=max(v2,v1);
retain v2;
run;``````

Result

```Obs    ID    Year    V1    v2

1      1    2002     0     0
2      2    2000     0     0
3      2    2001     1     1
4      2    2002     0     1
5      3    2005     0     0
6      3    2006     1     1
7      3    2007     1     1
```
Discussion stats
• 2 replies
• 424 views
• 2 likes
• 3 in conversation