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

Hi folks: 

I have the following varlists (_effectentered and _effectremoved). 

I want to have another varlist (_stdvar) which excludes &_effectremoved from &_effectentered.

I tried the following code but the problem is that x2 will be dropped from x23 as well.

 

%let _EffectEntered = x1 x2 x3 x23 ;
%let _EffectRemoved = x2 x3 ;
%let _stdvar=%sysfunc(prxchange(s/%sysfunc(translate(&_EffectRemoved,%str(|),%str( )))//,-1,&_EffectEntered));
%put &_stdvar ;

 

would you please help me in this regard?

Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

Hi Moh,

 

Good idea to use PRXCHANGE here, so you can perform this fairly complex operation within the %LET statement.

 

I think you should insert word boundary metacharacters and put the "or" expression in parentheses to make sure that only complete words are replaced, not substrings of longer words:

%let _stdvar=%cmpres(%sysfunc(prxchange(s/\b(%sysfunc(translate(&_EffectRemoved,%str(|),%str( ))))\b//,-1,&_EffectEntered)));
%put &_stdvar;

The %CMPRES function (or macro) replaces multiple blanks by single blanks.

View solution in original post

3 REPLIES 3
FreelanceReinh
Jade | Level 19

Hi Moh,

 

Good idea to use PRXCHANGE here, so you can perform this fairly complex operation within the %LET statement.

 

I think you should insert word boundary metacharacters and put the "or" expression in parentheses to make sure that only complete words are replaced, not substrings of longer words:

%let _stdvar=%cmpres(%sysfunc(prxchange(s/\b(%sysfunc(translate(&_EffectRemoved,%str(|),%str( ))))\b//,-1,&_EffectEntered)));
%put &_stdvar;

The %CMPRES function (or macro) replaces multiple blanks by single blanks.

Astounding
Opal | Level 21

Essentially, this will be the same problem as one that was recently answered:

 

https://communities.sas.com/t5/SAS-Data-Management/How-to-merge-two-varlists/m-p/242534

 

Assuming that it is OK to uppercase the variable names:

 

%let _stdvar=;

%let _EffectEntered = %upcase(&_EffectEntered);

%let _EffectRemoved = %upcase(&_EffectRemoved);

 

%do i=1 %to %sysfunc(countw(&_EffectEntered));

    %let nextone = %scan(&_EffectEntered, &i);

    %if %index( %str( &_EffectRemoved ), %str( &nextone )) = 0 %then %let _stdvar = &_stdvar &nextone;

%end;

 

The usual warnings apply.  You will need to define a macro since the code uses %IF.  In that case, you need to pay attention to which macro variables are local and which are global.

 

Good luck.

SAS INNOVATE 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Get the $99 certification deal.jpg

 

 

Back in the Classroom!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 993 views
  • 2 likes
  • 3 in conversation