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-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!

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.

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
  • 2 replies
  • 922 views
  • 2 likes
  • 2 in conversation