Hello
I have the following dataset;
data have;
input id 1. term $8. ca 1.;
datalines;
1 199602 1
1 199603 0
1 199701 1
1 199702 1
1 199703 0
1 199802 1
2 200501 1
2 200502 1
2 200503 0
2 200601 0
2 200602 0
3 201001 1
3 201002 1
3 201003 0
3 201101 1
3 201102 1
;
run;
I want it recoded to the following;
data want;
input id 1. term $8. ca 1.;
datalines;
1 199602 1
1 199603 1
1 199701 1
1 199702 1
1 199703 1
1 199802 1
2 200501 1
2 200502 1
2 200503 0
2 200601 0
2 200602 0
3 201001 1
3 201002 1
3 201003 1
3 201101 1
3 201102 1
;
run;
Basically, i want to recode id=1 and id=3 because both those ids have 1 in the first and 1 in the last term and therefore, if there are zeros in the in-between terms, i want to recode them into one.
Please help!
Thanks
ananda
data have;
input id 1. term $8. ca 1.;
datalines;
1 199602 1
1 199603 0
1 199701 1
1 199702 1
1 199703 0
1 199802 1
2 200501 1
2 200502 1
2 200503 0
2 200601 0
2 200602 0
3 201001 1
3 201002 1
3 201003 0
3 201101 1
3 201102 1
;
run;
data want;
do _n_=1 by 1 until(last.id);
set have;
by id;
if first.id then _n=ca;
end;
_n=ca & _n;
do _n_=1 to _n_;
set have;
if _n then ca=_n;
output;
end;
drop _:;
run;
data have;
input id 1. term $8. ca 1.;
datalines;
1 199602 1
1 199603 0
1 199701 1
1 199702 1
1 199703 0
1 199802 1
2 200501 1
2 200502 1
2 200503 0
2 200601 0
2 200602 0
3 201001 1
3 201002 1
3 201003 0
3 201101 1
3 201102 1
;
run;
data want;
do _n_=1 by 1 until(last.id);
set have;
by id;
if first.id then _n=ca;
end;
_n=ca & _n;
do _n_=1 to _n_;
set have;
if _n then ca=_n;
output;
end;
drop _:;
run;
Thank you very much for your quick response!
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.