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

Hi everyone,

 

I have a problem that I hope someone could help with.  I'm fairly new to SAS (9.4) so I apologize if my description is not clear and does not use the appropriate SAS terminology.  Here is the problem:

 

I have 2 SAS data sets - data set A contains over 500 words and data set B contains over 10,000 complex character strings.  I would like to write a program that will evaluate every word data set B to see if it exists in data set A.  If it does, then replace the word with something else (the replacement is abritrary) and put the final result in data set C.  For example, if the replacement word is HAT, then:

 

Data Set A                  

1  CAR

2  DOG

3  MAN

 

Data Set B

1  THE DOG RODE IN THE CAR WITH THE MAN

2  THIS CHEESEBURGER IS GREAT

 

Data Set C

1  THE HAT RODE IN THE HAT WITH THE HAT

2  THIS CHEESEBURGER IS GREAT

 

I've tried finding a solution to this problem in this message board and others but have not found a satisfactory solution.  Any help will be greatly appreciated.  Thanks in advance!

1 ACCEPTED SOLUTION

Accepted Solutions
Haikuo
Onyx | Level 15

I suppose it could be more efficient if we load table 'A' into a Hash object, but no comparison is done. Here is an old school way of doing it:

data a;
	input id words$;
	cards;
1  CAR
2  DOG
3  MAN
;

data b;
	infile cards truncover;
	input b_id sentences$100.;
	cards;
1  THE DOG RODE IN THE CAR WITH THE MAN
2  THIS CHEESEBURGER IS GREAT
;

data want;
	set b;

	do i=1 to nobs;
		set a nobs=nobs point=i;
		
		sentences=prxchange(cats('s/\b',words,'\b/HAT/'),-1,sentences);
	end;

	keep b_id sentences;
run;

View solution in original post

2 REPLIES 2
Haikuo
Onyx | Level 15

I suppose it could be more efficient if we load table 'A' into a Hash object, but no comparison is done. Here is an old school way of doing it:

data a;
	input id words$;
	cards;
1  CAR
2  DOG
3  MAN
;

data b;
	infile cards truncover;
	input b_id sentences$100.;
	cards;
1  THE DOG RODE IN THE CAR WITH THE MAN
2  THIS CHEESEBURGER IS GREAT
;

data want;
	set b;

	do i=1 to nobs;
		set a nobs=nobs point=i;
		
		sentences=prxchange(cats('s/\b',words,'\b/HAT/'),-1,sentences);
	end;

	keep b_id sentences;
run;
DSW
Calcite | Level 5 DSW
Calcite | Level 5

Wow, this is great.  Thank you so much, it works beautifully.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 2 replies
  • 1081 views
  • 2 likes
  • 2 in conversation