## Check and combine multiple columns into one

Solved
Frequent Contributor
Posts: 82

# Check and combine multiple columns into one

To generate datasets needed:

``````data have;
input word_a \$ word_b \$ WoRD_c \$ ignore_d \$;
datalines;
. Test TEST ignore
;
run;

data want;
input WORD \$ ignore_d \$ WORD_IND;
datalines;
TEST ignore 0
;
run;

data have2;
input num_a num_b Num_c ignore_d \$;
datalines;
. 2 3 ignore
;
run;

data want2;
input NUM ignore_d \$ NUM_IND;
datalines;
. ignore 1
;
run;``````

From dataset 'have' to 'want':

there are 3 columns whose names have 'word' inside, need to compare the values. If the values are the same (after upcase it, and ignoring blank value), then output a single column named 'WORD' and the indicator 'WORD_IND' is 0.

From dataset 'have2' to 'want2':

there are 3 columns whose names have 'num' inside, compare the values. Because the values are not the same (ignoring blank value), output a single column named 'NUM' but with blank value, and the indicator 'NUM_IND' is 1.

How to achieve it?

Accepted Solutions
Solution
‎05-18-2017 02:00 AM
Posts: 1,163

## Re: Check and combine multiple columns into one

``````/*dataset 1*/
data have;
input word_a \$ word_b \$ WoRD_c \$ ignore_d \$;
datalines;
unk Test TEST ignore
;
run;

data want1;
set have;
array wrd word_a  word_b  WoRD_c;
do over wrd;
wrd=upcase(wrd);
end;
run;

data want2;
set want1;
call sortc (of word:);
word_ind= ifc(coalescec(of word:) eq word_c, '0' , '1');
set want1;
run;

/*dataset 2*/
data have2;
input num_a num_b Num_c ignore_d \$;
datalines;
. 2 3 ignore
;
run;

data want2;
set have2;
call sortn (of num:);
NUM_IND = ifn(coalescec(of num:) eq num_c, 0 , 1);
set have2;
run;``````
Thanks,
Jag

All Replies
Super User
Posts: 23,998

## Re: Check and combine multiple columns into one

Do you have only the two variables that will be created, Word or NUM or will it depend on the field?

If the variable name created is dependent on the data you're going to need a macro.

Frequent Contributor
Posts: 82

## Re: Check and combine multiple columns into one

Hey Reeza,
Yes I have only the two variables that will be created.
Super User
Posts: 23,998

## Re: Check and combine multiple columns into one

@ayin How much information will you have ahead of time or do you need a solution as provided by @Jagadishkatam. Also, do you have only the three words or is there a possibility of more variables to check?

Frequent Contributor
Posts: 82

## Re: Check and combine multiple columns into one

The current solution will suffice for the moment, and I agree with you that a macro could be a better option.
Solution
‎05-18-2017 02:00 AM
Posts: 1,163

## Re: Check and combine multiple columns into one

``````/*dataset 1*/
data have;
input word_a \$ word_b \$ WoRD_c \$ ignore_d \$;
datalines;
unk Test TEST ignore
;
run;

data want1;
set have;
array wrd word_a  word_b  WoRD_c;
do over wrd;
wrd=upcase(wrd);
end;
run;

data want2;
set want1;
call sortc (of word:);
word_ind= ifc(coalescec(of word:) eq word_c, '0' , '1');
set want1;
run;

/*dataset 2*/
data have2;
input num_a num_b Num_c ignore_d \$;
datalines;
. 2 3 ignore
;
run;

data want2;
set have2;
call sortn (of num:);
NUM_IND = ifn(coalescec(of num:) eq num_c, 0 , 1);
set have2;
run;``````
Thanks,
Jag
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
• 5 replies
• 789 views
• 2 likes
• 3 in conversation