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

Hi,

I have following question, and respondent can check all that apply thus multiple categories (see data). How can I break multiple responses such as 1,4,5,6 into separate question and response?  Thank you very much! Haiyi

 

What TV network did you watch? Please check all that apply

▢           NBC  (1)

▢           CBS (2)

▢           CNN  (3)

▢           Fox  (4)

▢           ABC (5)

▢           pbs  (6)

 

 

Obs       network

1            1,4,5

2            1,4,5,6,

3            2

4            1,3,4

5            1,3,4,5,6

 

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star
First you need a plan.
Right now you have a single variable called NETWORK. When this is done, do you want 6 variables instead (such as Network1 through Network6, all either "Y" or "N")? Do you have something else in mind?

View solution in original post

3 REPLIES 3
ballardw
Super User

How really depends on what your current data SAS data set looks like and what you expect the output to look like.

Typically with something like this I prefer to have variables that are coded 1 (for selected) and 0 (not selected).

If that is what you want you might look at the survey software used as that may be an Export option (I know at least 4 different software packages this is the case.) and avoid the whole issue.

 

Or maybe something like this:

data have;
   input obs network :$10.;
datalines;
1            1,4,5
2            1,4,5,6
3            2
4            1,3,4
5            1,3,4,5,6
;

data want;
   set have;
   array n   NBC CBS  CNN  Fox 
             ABC PBS;
   /* set all the neworks to 0*/
   do i= 1 to dim(n);
      n[i] = 0;
   end;
   /* if the networks number is 
      in the value then set the 
      value of the individual network variable to
      1
   */
  do i=1 to countw(network,',');
     n[input(scan(network,i),best.)] = 1;
  end;
  drop i;
run; 

 

Arrays are a way to reference multiple variables that you intend to do something similar with each.

You reference the individual variable by using the array name, N above, followed by an index or position value in parentheses or brackets. N(3) above would be the CNN variable.

The DIM function returns the number of elements (variables) in an array. So if you add or remove a variable in the Array definition the code adjusts.

 

The Countw function as used returns the number of elements separated by commas. CAUTION: If you actually have commas with no value afterward then the array usage will return an error.

 

The fancy code in this: n[input(scan(network,i),best.)]  uses the Scan function to get one of the "numbers" from the list and then uses the Input function to create a numeric value so the array index is appropriate. Indexes for arrays must be integers so if you have something that causes values like 1, 2.4, 6 there will be an error.

 

 

Astounding
PROC Star
First you need a plan.
Right now you have a single variable called NETWORK. When this is done, do you want 6 variables instead (such as Network1 through Network6, all either "Y" or "N")? Do you have something else in mind?
JulieLearnsSAS
Fluorite | Level 6

 

After considering the other answers you've been given, you will probably want to think of something like this:  https://communities.sas.com/t5/SAS-Programming/How-to-get-all-possible-multiplicative-interactions/t...

 

Best of luck, 

 

Julez.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 561 views
  • 3 likes
  • 4 in conversation