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

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
Tom
Super User Tom
Super User

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

View solution in original post

2 REPLIES 2
PeterClemmensen
Tourmaline | Level 20
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

 

Tom
Super User Tom
Super User

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
How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 1384 views
  • 2 likes
  • 3 in conversation