SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
_maldini_
Barite | Level 11

I am analyzing data from a survey with questions whose answers are in the form of checkboxes (i.e. "Check all that apply"). In the raw data, each checkbox (i.e. Response) is its own variable (See below: addiction___1 is the first checkbox, addiction___2 is the second checkbox, etc.). I would like to create a new variable for each question and have each checkbox / response be its own category / level in that variable. 

 

The code below works fine when I am coding one question at a time. Is there a way to automate / condense the syntax so I don't have to write this syntax for each question over and over again?!

 

	IF addiction___1 THEN DO;
	addiction = 1;
	
	END;
	
	IF addiction___2 THEN DO;
	addiction = 2;
	
	END;
	
	IF addiction___3 THEN DO;
	addiction = 3;
	
	END;
	
	IF addiction___4 THEN DO;
	addiction = 4;
	
	END;
	
	IF addiction___5 THEN DO;
	addiction = 5;
	
	END;
1 ACCEPTED SOLUTION

Accepted Solutions
hashman
Ammonite | Level 13

@_maldini_:

 

You're saying "I would like to create a new variable for each question", yet it's not what your code is doing. Rather, it overwrites the value of the ADDICTION variable with a new value X every time the next addiction__X evaluates true. If this is what you really want, you can shorten your code by using:

array add addiction__1-addiction__5 ;

do over add ;
  if add then addiction = _i_ ;
end ;  

 This way, if none of the boxes is checked, the variable ADDICTION will remain missing.

 

However, if instead you want, as you say, to create a new variable for each box and populate variable #1 with 1 is addiction__1 is true, variable #2 - with 2 if  addiction__2 is true, and so forth, you'll have to do it differently; for example:

array add addiction__1-addiction__5 ;
array new newvar_1-newvar_5 ;

do over add ;
  if add then new = _i_ ;
end ;

This way, if you had the addiction__1-addiction__5 variables valued, for example, as:

 

(unchecked, checked, unchecked, unchecked, checked)  

 

you'd get newvar_1-newvar_5 populated as:

 

(. 2 . . 5)

 

But the approach depends on what you really want to get. Unfortunately, you're a bit vague on that head, and discerning your intent from your sample code is uncertain.

 

Paul D. 

View solution in original post

4 REPLIES 4
Astounding
PROC Star

I'm not sure that this code is really working fine.  When both 1 and 2 are checked, your new variable ADDICTION will be 2 and won't capture the fact that 1 was also checked.  

VDD
Ammonite | Level 13 VDD
Ammonite | Level 13

You should use the /debug on your data statement.

See what is happening yourself.

 

data fun / debug;

     /* lots of stuff you want to check */

run:

 

hashman
Ammonite | Level 13

@_maldini_:

 

You're saying "I would like to create a new variable for each question", yet it's not what your code is doing. Rather, it overwrites the value of the ADDICTION variable with a new value X every time the next addiction__X evaluates true. If this is what you really want, you can shorten your code by using:

array add addiction__1-addiction__5 ;

do over add ;
  if add then addiction = _i_ ;
end ;  

 This way, if none of the boxes is checked, the variable ADDICTION will remain missing.

 

However, if instead you want, as you say, to create a new variable for each box and populate variable #1 with 1 is addiction__1 is true, variable #2 - with 2 if  addiction__2 is true, and so forth, you'll have to do it differently; for example:

array add addiction__1-addiction__5 ;
array new newvar_1-newvar_5 ;

do over add ;
  if add then new = _i_ ;
end ;

This way, if you had the addiction__1-addiction__5 variables valued, for example, as:

 

(unchecked, checked, unchecked, unchecked, checked)  

 

you'd get newvar_1-newvar_5 populated as:

 

(. 2 . . 5)

 

But the approach depends on what you really want to get. Unfortunately, you're a bit vague on that head, and discerning your intent from your sample code is uncertain.

 

Paul D. 

ballardw
Super User

You may want to visit this thread: https://communities.sas.com/t5/Base-SAS-Programming/consolidating-columns/m-p/484770#M125874

 

Which has a better description of starting data and I believe at heart is similar to your problem with possibly yours not having an issue with identical responses appearing in multiple variables.

 

Or perhaps you can provide a more explicit example of existing data set and what you want.

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

Register now!

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
  • 4 replies
  • 3219 views
  • 5 likes
  • 5 in conversation