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

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?

 

1 ACCEPTED SOLUTION

Accepted Solutions
Jagadishkatam
Amethyst | Level 16
/*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

View solution in original post

5 REPLIES 5
Reeza
Super User

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
Quartz | Level 8
Hey Reeza,
Yes I have only the two variables that will be created.
Reeza
Super User

@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?

ayin
Quartz | Level 8
The current solution will suffice for the moment, and I agree with you that a macro could be a better option.
Jagadishkatam
Amethyst | Level 16
/*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

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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