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

Hi

my data looks like this:

ID YEAR "VARIABLE A"  

1    1995    0

 1    1996    0

1     1997     0

1     1998     0

1     1999     1

1      2000     1

1      2001      0

1     2002      0

2    1995      1

I want for the same ID, to change the value of "variable a" to 1 only for the obs that are shown bellow "variable a"=1.

That is, I want to get this:

 

ID YEAR "VARIABLE A"  

1    1995    0

 1    1996    0

1     1997     0

1     1998     0

1     1999     1

1      2000     1

1      2001      1

1     2002     1

2    1995      1

 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

If I assume your rule correctly, this will do it:

data have;
input id year var_a;
datalines;
1 1995 0
1 1996 0
1 1997 0
1 1998 0
1 1999 1
1 2000 1
1 2001 0
1 2002 0
2 1995 1
;

data want;
set have (rename=(var_a=_var_a));
by id;
retain var_a;
if first.id then var_a = _var_a;
var_a = max(_var_a,var_a);
drop _var_a;
run;

Note how example data is presented in a data step with datalines; do so yourself in the future.

View solution in original post

1 REPLY 1
Kurt_Bremser
Super User

If I assume your rule correctly, this will do it:

data have;
input id year var_a;
datalines;
1 1995 0
1 1996 0
1 1997 0
1 1998 0
1 1999 1
1 2000 1
1 2001 0
1 2002 0
2 1995 1
;

data want;
set have (rename=(var_a=_var_a));
by id;
retain var_a;
if first.id then var_a = _var_a;
var_a = max(_var_a,var_a);
drop _var_a;
run;

Note how example data is presented in a data step with datalines; do so yourself in the future.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 1 reply
  • 360 views
  • 1 like
  • 2 in conversation