- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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:
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.