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

Hello,

I have two macro variables(list_1 and list_2). A list of words have been assigned to each of the variable.
I need to reset list_2 by removing any words that appear in list_1.

%let list_1  = google microsoft yahoo;
%let list_2  = all cnn microsoft fox mtv google;

I need:

list_2_RESET = all cnn fox mtv;

Please advice.

Thanks for your help.

1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

Using &list_1 you could create a regular expression and then use this regular expression to remove words in &list_2 as demonstrated with below code:

%let list_1  = google microsoft yahoo;
%let list_2  = all cnn microsoft fox mtv google;

/* create regex string */
%let list_1_regex=\b%sysfunc(prxchange(s/ +/ ?\b|\b/oi,-1,&list_1))\b;

/* use SAS regex function prxchange() to remove words from &list_2 */

%let list_2_DIFF=%sysfunc(prxchange(s/&list_1_regex//oi,-1,&list_2));

%put list_1_regex=&list_1_regex;

%put list_2_DIFF=&list_2_DIFF;

View solution in original post

5 REPLIES 5
ballardw
Super User

Is the data case sensitive? For example do you fox, Fox and/or FOX in your data and are they considered to be the same? Do you have any compound words to consider such as All Star needing to be treated as one word?

If they come from a data set it may be better to leave them there and use sql with the except operator on distinct values of the variable(s).

P_S_
Obsidian | Level 7

This scenario is just a part of the bigger code that I use. After everything is said and done, there are values assigned to the list_1 and list_2 macro vars like I mentioned in the post. So now I need to remove the values if they are in list_1. The case doesn't matter. Lets assume its all lower case.

Patrick
Opal | Level 21

Using &list_1 you could create a regular expression and then use this regular expression to remove words in &list_2 as demonstrated with below code:

%let list_1  = google microsoft yahoo;
%let list_2  = all cnn microsoft fox mtv google;

/* create regex string */
%let list_1_regex=\b%sysfunc(prxchange(s/ +/ ?\b|\b/oi,-1,&list_1))\b;

/* use SAS regex function prxchange() to remove words from &list_2 */

%let list_2_DIFF=%sysfunc(prxchange(s/&list_1_regex//oi,-1,&list_2));

%put list_1_regex=&list_1_regex;

%put list_2_DIFF=&list_2_DIFF;

P_S_
Obsidian | Level 7

Wow. I would have never thought of using this technique. Thanks a lot Patrick.

Haikuo
Onyx | Level 15

So slick! Million times better than the one I had in mind, if I had one.
Thanks, Patrick!

Haikuo

sas-innovate-2024.png

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.

 

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
  • 5 replies
  • 2059 views
  • 4 likes
  • 4 in conversation