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
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.
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.
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and save with the early bird rate—just $795!
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.
Ready to level-up your skills? Choose your own adventure.