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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

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.

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
  • 5 replies
  • 3246 views
  • 2 likes
  • 3 in conversation