Hi all,
I have a dataset with a variable that has different categories. I need to create a binary (yes/no) for each category.
Data Have:
| ID | D_name |
| 1 | a |
| 1 | a,b |
| 2 | a |
| 2 | a,b,c |
| 2 | a |
| 3 | a |
| 3 | b |
| 3 | c |
| 4 | a |
| 4 | c |
Data want:
| ID | a_category | b_category | c_category |
| 1 | yes | yes | no |
| 2 | yes | yes | yes |
| 3 | yes | yes | yes |
| 4 | yes | no | yes |
data have;
input ID D_name $;
cards;
1 a
1 a,b
2 a
2 a,b,c
2 a
3 a
3 b
3 c
4 a
4 c
;
data want;
do until(last.id);
set have;
by id;
array t $3 a_category b_category c_category ;
array j(3) $ _temporary_ ('a','b','c') ;
do _n_=1 to dim(j);
if t(_n_) ne 'yes' then t(_n_)=ifc( index(d_name,strip(j(_n_))),'yes','no');
end;
end;
drop d_name;
run;
data have;
input ID D_name $;
cards;
1 a
1 a,b
2 a
2 a,b,c
2 a
3 a
3 b
3 c
4 a
4 c
;
data want;
do until(last.id);
set have;
by id;
array t $3 a_category b_category c_category ;
array j(3) $ _temporary_ ('a','b','c') ;
do _n_=1 to dim(j);
if t(_n_) ne 'yes' then t(_n_)=ifc( index(d_name,strip(j(_n_))),'yes','no');
end;
end;
drop d_name;
run;
data want;
set have;
by id;
length d_cat $10.;
retain d_cat ;
array cat[3] $ a_category b_category c_category;
if first.id then call missing(d_cat);
d_cat=cats(d_cat,D_name);
if last.id then
do;
do _n_=1 to dim(cat);
if find(d_cat,scan(vname(cat[_n_]),1,'_')) then
cat[_n_]='yes';
else cat[_n_]='no';
end;
output;
end;
drop D_name;
run;
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.