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

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
1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

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;

View solution in original post

2 REPLIES 2
novinosrin
Tourmaline | Level 20

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;
r_behata
Barite | Level 11

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;
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
  • 1038 views
  • 1 like
  • 3 in conversation