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

Hi, I want to process a comma-separated list of values in a variable (e.g., Q8) and create corresponding response variables (Q8_1, Q8_2, ..., Q8_17). For each value in the list, I want to set the corresponding response variable to 1 if there is a value. If the value is missing, the corresponding response variable should remain blank. Here is an example of what the variable initially looks like:

Q8

11

.

.

.

15,17

.

11,12,13,14,15

1,2,3,4,5,6,7,8,9,10

11

 

I tried something like this, but it's not accounting correctly for values 10+. any suggestions? I am not married to this code, so if someone has a completely different one, I am okay with that.

 

data numeric2;
set numeric;
run;
%macro initial2(var, num);
data numeric2;
format &var._1 - &var._&num 8.;
set numeric2;
run;
%mend;
%macro pop_var(var, num);
data numeric2;
set numeric2;
if index(&var, "&num")>0 then &var._&num = 1;
run;
%mend pop_var;
 
%macro run_pop(var2, num2);
%initial2(&var2, &num2);
%do i = 1 %to &num2;
%pop_var(&var2, &i);
%end;
%mend;
 
%run_pop(Q8, 17);
 

 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

@smh92 wrote:

Thanks so much! Do you know why it's only creating the variables q8_1-q8_17 but not populating 1 or blank?


You would have to provide data in the form of working data step AND the code that you actually ran to actually answer that.

Possibly include the log with the code and any messages might provide a clue but data is the key.

 

 

My approach to YOUR requirement:

data example;
   length q8 $ 55;
   input q8 $;
datalines;
11
.
.
.
15,17
.
11
11,12,13,14,15
1,2,3,4,5,6,7,8,9,10
;

data want;
   set example;
   array q(*) q8_1 - Q8_17;
   if not missing(q8) then do i=1 to countw(q8);
      index= input(strip(scan(q8,i)),5.);
      q[index]=1;
   end;
run;

 


 

View solution in original post

4 REPLIES 4
PaigeMiller
Diamond | Level 26

I think an ARRAY is a much better choice here at working with many columns in a data set, than macros.

 

data want;
    set have;
    array q8 q8_1-q8_17;
    do i=1 to countw(string,',');
    	do j=1 to 17;
    	    if scan(string,i,',')=j then q8(j)=1;
	    end;
    end;
    drop i j;
run;


This sets the variables to 1 if that value is found, and missing if it is not found.

--
Paige Miller
smh92
Calcite | Level 5

Thanks so much! Do you know why it's only creating the variables q8_1-q8_17 but not populating 1 or blank?

ballardw
Super User

@smh92 wrote:

Thanks so much! Do you know why it's only creating the variables q8_1-q8_17 but not populating 1 or blank?


You would have to provide data in the form of working data step AND the code that you actually ran to actually answer that.

Possibly include the log with the code and any messages might provide a clue but data is the key.

 

 

My approach to YOUR requirement:

data example;
   length q8 $ 55;
   input q8 $;
datalines;
11
.
.
.
15,17
.
11
11,12,13,14,15
1,2,3,4,5,6,7,8,9,10
;

data want;
   set example;
   array q(*) q8_1 - Q8_17;
   if not missing(q8) then do i=1 to countw(q8);
      index= input(strip(scan(q8,i)),5.);
      q[index]=1;
   end;
run;

 


 

smh92
Calcite | Level 5

This works. Thanks again for your help!

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 4 replies
  • 558 views
  • 0 likes
  • 3 in conversation