BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
anandas
Obsidian | Level 7

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

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

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;

View solution in original post

2 REPLIES 2
novinosrin
Tourmaline | Level 20

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;
anandas
Obsidian | Level 7

Thank you very much for your quick response!

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
  • 1050 views
  • 0 likes
  • 2 in conversation