BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
mraposa1
Calcite | Level 5

I have a dataset from a survey where one of the questions was select all that apply. When exported from Qualtrics to Excel, the variable is in this format:

 

Obs

Covid

1

1,10,11,12,13,14

2

1,12

 

There are 16 potential response options. I am stuck how to create binary variables for each response option without hardcoding everything in excel. I ultimately want to determine how many observations selected each response option. 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User
data have;
infile cards dsd;
input obs $ COVID : $20.;
cards;
"1", "1,10,11,12,13,14"
"2", "1,12"
;;;;
run;

data binary;
set have;

nwords = countw(covid);

array _covid(14) covid1-covid14 ;

*set all to 0;
do i=1 to 14;
_covid(i) = 0;
end;

*set to 1 for each value of the COVID variable, ie COVID1=1, COVID10=1, COVID11=1 etc;
do i=1 to nwords;
	index = input(scan(covid, i), 8.);
	_covid(index) = 1;
end;

drop nwords index i;
run;
 

@mraposa1 wrote:

I have a dataset from a survey where one of the questions was select all that apply. When exported from Qualtrics to Excel, the variable is in this format:

 

Obs

Covid

1

1,10,11,12,13,14

2

1,12

 

There are 16 potential response options. I am stuck how to create binary variables for each response option without hardcoding everything in excel. I ultimately want to determine how many observations selected each response option. 


 

View solution in original post

3 REPLIES 3
Reeza
Super User
data have;
infile cards dsd;
input obs $ COVID : $20.;
cards;
"1", "1,10,11,12,13,14"
"2", "1,12"
;;;;
run;

data binary;
set have;

nwords = countw(covid);

array _covid(14) covid1-covid14 ;

*set all to 0;
do i=1 to 14;
_covid(i) = 0;
end;

*set to 1 for each value of the COVID variable, ie COVID1=1, COVID10=1, COVID11=1 etc;
do i=1 to nwords;
	index = input(scan(covid, i), 8.);
	_covid(index) = 1;
end;

drop nwords index i;
run;
 

@mraposa1 wrote:

I have a dataset from a survey where one of the questions was select all that apply. When exported from Qualtrics to Excel, the variable is in this format:

 

Obs

Covid

1

1,10,11,12,13,14

2

1,12

 

There are 16 potential response options. I am stuck how to create binary variables for each response option without hardcoding everything in excel. I ultimately want to determine how many observations selected each response option. 


 

mraposa1
Calcite | Level 5

Thank you so much! I appreciate it!

Tom
Super User Tom
Super User

You can also loop the other way. Loop over the codes 1 to 14 and check if they are in the list or not.

data binary;
  set have;
  array _covid covid1-covid14 ;
  do index=1 to 14 ;
     _covid[index] = 0 <  indexw(covid,cats(index),', ')  ;
  end;
  drop index;
run;

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

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 lock in 2025 pricing—just $495!

Register now

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1497 views
  • 2 likes
  • 3 in conversation