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?
/*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;
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.
@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?
/*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;
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.
What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.
Find more tutorials on the SAS Users YouTube channel.