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

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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